Help With Integrating Servlet and JSP Page?

Hello There
--i made jsp page that contain name and description fields and add button
--and i made servlet that contain the code to insert name and description in the database
--and i want to make that when the user hit the add button
-->the entered name and description is sent to the servlet
and the servlet sent them to database?
here's what i 've done:
the jsp code:
<html:html locale="true">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>
        Categories Page
       </title>
        <html:base/>
    </head>
    <body style="background-color: white">
    <form action="jpage.jsp" method="get">
        <h1>
            <center>
categories Operations
            </center>
        </h1>
        <h3>
             <label>Name</label>
        <input type="text" name="name" value="" size="10" />
             <label>Description</label>
         <input type="text" name="description" value="" size="10" />
         <input type="submit" value="Add" name="button" />
       </h3>
   </form>
    </body>
</html:html>the servlet code:
import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.net.*;
class NewServlet1 extends HttpServlet{
     Connection conn;
     private ServletConfig config;
public void init(ServletConfig config)
  throws ServletException{
     this.config=config;
public void service (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
   HttpSession session = req.getSession(true);
   res.setContentType("text/html");
try{
     Class.forName("com.mysql.jdbc.Driver");
   conn = DriverManager.getConnection("jdbc:mysql://localhost/struts", "root", "");
     PreparedStatement ps;
   ps = conn.prepareStatement ("INSERT INTO categories (Name, Description) VALUES(?,?)");
      ps.setString (1, "aa");
      ps.setString (3, "bb");
      ps.executeUpdate();
      ps.close();
      conn.close();
  }catch(Exception e){ e.getMessage();}
  public void destroy(){}
}

The JSP Code:
<html:html locale="true">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>
        Categories Page
       </title>
        <html:base/>
    </head>
    <body style="background-color: white">
    <form action="actionServlet.do?action=Additem" method="*post*">
        <h1>
            <center>
categories Operations
            </center>
        </h1>
        <h3>
             <label>Name</label>
        <input type="text" name="name" value="" size="10" />
             <label>Description</label>
         <input type="text" name="description" value="" size="10" />
         <input type="button" value="Submit">
       </h3>
   </form>
    </body>
</html:html>The Servlet Code:
import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.net.*;
public class NewServlet1 extends HttpServlet implements SingleThreadModel {
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException {
        doPost(request,response);
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException {
          String action = request.getParameter("action"); // action = "Additem"
          if (action.equals("Additem")) {
               String name = request.getParameter("name");
               String description = request.getParameter("description");
                     RequestDispatcher reqDisp = null;
               try{
              Connection conn;
              PreparedStatement ps;
     Class.forName("com.mysql.jdbc.Driver");
   conn = DriverManager.getConnection("jdbc:mysql://localhost/struts", "root", "");
   ps = conn.prepareStatement ("INSERT INTO categories (Name, Description) VALUES(?,?)");
      ps.setString (1, name);
      ps.setString (3, description);
      ps.executeUpdate();
      ps.close();
      conn.close();
      reqDisp= request.getRequestDispatcher("./index.jsp");
      reqDisp.forward(request, response);
               catch (Exception ex){
                    System.out.println("Error: "+ ex);
}The web.xml code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>2</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>2</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
        </servlet>
    <servlet>
        <servlet-name>NewServlet1</servlet-name>
        <servlet-class>NewServlet1</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>NewServlet1</servlet-name>
        <url-pattern>/NewServlet1</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
        <servlet>
     <servlet-name>actionServlet</servlet-name>
     <servlet-class>com.test.servlet.NewServlet1</servlet-class>
</servlet>
<servlet-mapping>
     <servlet-name>actionServlet</servlet-name>
     <url-pattern>*.do</url-pattern>
</servlet-mapping>
    </web-app>

Similar Messages

  • Help with buttons on a JSP Page

    Hi All,
    I have a HTML page , that has certain text fields.... on entering values into the text fields, it is directed to a servlet.
    The servlet(puts the values into the database) then directs to a jsp with the values entered in the form.
    I would like to know whether on the jsp page i can have 2 buttons like "edit" and "OK".
    (how do I code it???)
    The "edit" button edits the information on the initial HTML page and
    The "OK" button just confirms the inputted information.
    I hope I have asked a clear question.It would be great if someone directs me in the right direction.
    Thanking everyone in advance
    AS

    I hope I've understood your question correctly. Let
    me see if I got it right. So the first page is the
    form, where the user inputs the information, the
    second page will display this information that they
    inputted from the previous page and then have two
    buttons, one that says edit and the other that says
    ok. Upon hitting edit they're sent back to the form
    with the values already filled out, if they hit OK,
    the data is inserted into the database. If this is
    correct, this is how I would do it.
    You will have 3 files
    parameters.jsp
    <%
    String firstname =
    = request.getParameter("firstname");
         String lastname = request.getParameter("lastname");
    %>form.jsp
    <%@ include file="parameters.jsp">
    <html>
         <body>
              <form action="confirmation.jsp" method="post">
    <input type="text" name="firstname" value="<%=
    <%= firstname %>">
    <input type="text" name="lastname" value="<%=
    <%= lastname %>">
                   <input type="submit">
              </form>
         </body>
    </html>confirmation.jsp
    <%@ include file="parameters.jsp">
    <html>
         <body>
    <!-- the action of this form will be the page that
    at inserts the data into the database -->
              <form action="insertintodb.jsp" method="post">
    <input type="text" name="firstname" value="<%=
    <%= firstname %>">
    <input type="text" name="lastname" value="<%=
    <%= lastname %>">
                   <input type="submit" value="OK">
              </form>
              <form action="form.jsp" method="post">
    <input type="hidden" name="firstname" value="<%=
    <%= firstname %>">
    <input type="hidden" name="lastname" value="<%=
    <%= lastname %>">
                   <input type="submit" value="Edit">
              </form>
         </body>
    </html>
    Hi,
    Thanks a lot for the information.I will try to do as you have suggested.Will post when i go into problems...
    Many thanks
    AS

  • Help with Jtext fields on JSP page?

    Hi All,
    How can I collect an integer on a JSP page from user input?
    I am looking to vallidate the user input also thought about vallidating using HTML then passing/casting the input to a Java variable for further manipulation on the page.
    I cannot use response.encodeUrl for this.
    Effectively what I would like to do is replicate what a JTextField with an action listener would do to retrieve user input in a desktop Java application.
    Any ideas and help would be welcome.
    regards
    Jim Ascroft

    Hi All ,
    I propbably didnt put my questions all that well.
    So I wil try to be more specific.
    1.How can I collect an integer on a JSP page from user input?
    I am looking to vallidate the user input. I thought about vallidating using HTML then passing/casting the input to a Java variable for further manipulation on the page.
    I cannot use response.encodeUrl for this.
    2.I understand that my Java helper classes are compiled into .class files and then used by the JSP page as required. Also the Java code within the <% %> Java tags is recognised and runs .
    So if the java between the <% %> tags can be compiled and used why can other Java components eg Swing or AWT not be used in the same way?
    Effectively what I would like to do is replicate what a JTextField with an action listener would do to retrieve user input in a desktop Java application.
    Any ideas and help would be welcome.
    regards
    Jim Ascroft

  • Single Sign-on with Multiple Servlets and JSPs

    I am in the midst of attempting to logically tie together a number of our
              web applications under a single sign-on "umbrella". What we want is the
              following: for any n applications a user may have access rights for up to n
              of them. Once signed in, she has rights to visit any app to which she has
              permissions as long as her session is valid. Unfortunately, I'm having
              trouble seeing how to make this work given the documentation that I have.
              I've read thru the newsgroup in search of a solution, but I haven't seen
              anything geared toward this specific approach.
              Currently, each "application" (servlet) has a list of valid users via ACLs
              (we've implemented a RealmExtender, so we're not going via props file
              entries), and we let the browser pop-up window enforce the sign-on. This
              has worked exactly as we wish (single sign-on, etc.), for testing, but we'd
              really rather have our own form-based sign-on for production.
              To that end, we've done the following:
              1) implemented a JSP form-based sign-on (basically ripped off from the
              example provided by BEA), which does a "ServletAuthentication.weak()" check
              to confirm identity.
              2) placed the following code (essentially) within the service() method of
              our servlet superclass, which I thought would force another check. My
              intention is to disallow the user from "jumping into" an app thru a
              shortcut, and thereby bypassing security.
              HttpSession session = request.getSession(true);
              if (session.isNew()) {
              response.sendRedirect(welcomeURL);
              However, we can't get the form-based approach to mimic the functionality of
              the default browser pop-up: the sign-in doesn't seem to "follow" the user
              the way it did with the pop-up. Instead, when I come in thru our login
              page, the browser pop-up is still appearing when I click the link for an
              app for which to which I have permissions.
              Is the default browser pop-up doing something different that I should know
              about? Seems like this should be simple to do, but it's surprisingly subtle
              (or maybe I'm just clueless).
              TIA
              

    Well, if you want to hear my personal opinion:
    better stick to the cookie specification (http://wp.netscape.com/newsref/std/cookie_spec.html) and accept the constraint that cookies will only be send to domains that tail-match the domain-constraint specified in the set-cookie http response.
    Although this specification is not an official internet standard most browsers are implementing the cookie mechanism according to this specification.
    Unfortenately there's no option to specify that a cookie should be send to a list of servers and/or sub-domains.
    However one physical server can have multiple (FQDN) hostnames. So if you intend to send the cookie to a group of servers the best approach is to create a new (DNS) (sub-)domain exclusively for those servers.
    Theoretically (and also practically) it is possible to set cookies for multiple domains (by using a webservice that will set cookies on request of a caller). But that approach is dangerous:
    (1) not the server but the http client is defining the content of the cookie (= part of the http server response)
    (2) (unintended) many servers can obtain the cookie which will be send to all servers that reside in all (tail-matching sub-)domains; although most likely only one or two servers of each domain are intended recipients
    Regards, Wolfgang

  • Need help with JSF table and scrollable pages

    I need help regarding usage of <t:dataTable>
    I have a List on my managed bean. It has 50 records.
    I am displaying the first 10 records and need to implement paging.
    I hear that there is a faces taglibrary called tomahawk which provides a <t:dataTable> and a <t:dataScroller> to implement scrolling.
    Is there a sample implentation that I can take a look at to see how I can solve my problem??
    Any help is highly appreciated.
    Thanks
    Amol

    Check here: http://www.irian.at/myfaces/dataScroller.jsf;jsessionid=F3F50A51583FEEF38D968A4AF5DC949C

  • Help with HTML tags and web page creating

    I have a project that is supposed to use an HTML class we make. He has given us the basics but we have to fill it in. My question is: How do you code a value that has been passed to a method into an html tag. For instance we have one that is called makeEmail and it is supposed to place an email address on a webpage. Here is the method, I just need to figure out how to code it correctly. Any suggestions on how to do this?
    public void makeEmail(String address)
    webPage += "" + address + "";
    }

    An email tag in html is very similar to a hyperlink. The string you want the makeEmail method to create is as follows:
    <a href="mailto:(email address")>(email address or description or whatevber you want displayed on screen)</a>So using for example my email address, you use the following line:
    <a href="mailto:[email protected]">Send an email to Mr_Silly</a>It is very much worth looking into an html tutorial, try searching for one on the web, cos there a thousands out there. It is a very simple language to learn.
    :-)

  • Serving Java Servlets and JSP

    I have a small hosting company and was wondering what is required to be installed on a Win2k Server to host Java Servlets and JSP pages for a client of mine?

    Ah, so you just want to add a servlet engine to IIS5?
    Tomcat can be used as a plugin for IIS. Check out the Tomcat FAQs - somewhere in there you should find one relating to using Tomcat as an IIS plugin. They're far more comprehensive than I could ever hope to be on the matter!

  • Proxying servlets and JSPs with apache

    We have a couple of servlets within our application, the rest of it
    being JSPs.
    We are having a problems maintaining the session state across links from
    the JSPs to the servlets. It works fine with WL as the web server when
    the servlet picks up the same session as the JSPs, but when we are using
    the apache bridge, the servlet seems to have a new session.
    We are using WL5.1.0 SP6 on solaris.
    The JSPs are proxied by extension, and the servlets by location -
    everything under the servlet directory being directed to a servlet.
    Is there anything we need to do in the configuration to allow this to
    work?
    Thanks,
    Steve

    Core Servlets is pretty good. I have both parts.
    One day I'm actually going to read them.you mean these two right?
    Core Servlets and Javaserver Pages: Core Technologies, Vol. 2 (2nd Edition) (Core) by Marty Hall, Larry Brown, and Yaakov Chaikin
    Core Servlets and JavaServer Pages, Vol. 1: Core Technologies, Second Edition by Marty Hall and Larry Brown
    I should buy the Vol 1 first right?

  • Creating FOrums with Servlet and JSP

    Hi everyone,
    Is there a good resource that explains how to build a forum using servlets and JSP technology?
    Regards,
    Basil Mahdi

    please let me know here if you find any useful resources...I am in the process of writing my own searchable threaded forum for my company with JSP's/servlets. It's a very slow process at the moment though...if I ever finish it, I'll gladly share it.

  • AppMod pooling with Servlets and JSPs

    Issue:
    I have a servlet that I need to do some work on a BC4J View Object (myVO), like set the where clause, execute, etc. I want to perform these actions in the servlet, and then forward on to a .jsp page (mypage.jsp). Then I want to use the BC4J Data Tag library to present the data. Normally, the AppMod tag would be used to get ahold of an AppMod, but in my case, I want to use the specific AppMod that I prepared inside the servlet that forwarded to mypage.jsp.
    I have looked through the BC4J Data Tag library and it appears that the data tags are getting the AppMod from the page context. I looked at the AppMod tag and I couldn't see exactly where it was sticking the AppMod into the page context, but when I looked at the DataSource tag, it was retrieving the AppMod from the page context. Therefore, I am assuming the AppMod tag is sticking it into the page context.
    What I would like to be able to do is perform some work on the AppMod in my servlet, then forward to a .jsp. Once in the .jsp, I would like to reference the AppMod without having to use the AppMod tag, but reference the AppMod directly from the DataSource tag. This would allow me to completely manage the AppMod interaction in my servlet, thus centralizing that logic and avoiding multiple check-ins/check-outs of the AppMod. I could also avoid using stateful management of the AppMod.
    Any insights would be much appreciated!
    Thx,
    Eric

    Core Servlets is pretty good. I have both parts.
    One day I'm actually going to read them.you mean these two right?
    Core Servlets and Javaserver Pages: Core Technologies, Vol. 2 (2nd Edition) (Core) by Marty Hall, Larry Brown, and Yaakov Chaikin
    Core Servlets and JavaServer Pages, Vol. 1: Core Technologies, Second Edition by Marty Hall and Larry Brown
    I should buy the Vol 1 first right?

  • InterMedia Java Classes for Servlets and JSPs and Netscape

    I am using the interMedia Java Classes for Servlets and JSPs to upload and retrieve multimedia data. I have found that it is much more performant in Internet Explorer (5.5) than in Netscape Communicator (4.7). In fact, I cannot upload images larger than 10K at all using netscape. However, the same image can be uploaded into the same application using IE. Is this a known issue?
    Thanks in advance.

    Hi,
    We have successfully uploaded multimedia data in the giga-byte range (Quicktime and AVI files) at the same speed with both the Netscape (4.7n) and MS IE (4.n and 5.n) browsers. One thing we have noticed is that its very important to set the manual proxy settings correctly if you have an environment with a proxy server. If you don't, then uploads can go via the proxy server and, for some reason, that seems to take considerably longer. For example, suppose you are in the www.xyzco.com domain and are connecting to a host named webserver.www.xyzco.com, then specify webserver and webserver.www.xyzco.com (to cover both cases) in the "Do not use proxy servers for..." box in the manual proxy server configuration screen. Also, if you're using a tool such as JDeveloper that uses the host IP address in the debugger environment, then you also need to add the numeric-based (nnn.nnn.nnn.nnn) IP address to this list.
    Hope this helps.
    In order to better understand the variety of ways in which our customers are using interMedia, we'd be very interested in knowing a little more about your application, the interMedia functionality that you are using, and how you are developing and deploying your application. If you are able to help us, please send a short email message with some information about your application, together with any comments you may have, to the Oracle interMedia Product Manager, Joe Mauro, at [email protected] Thank you!
    Regards,
    Simon
    null

  • JDBC installation for servlets and jsp

    Hello All,
    I am trying to use JDBC for my web application. i have a book but not very detailed and cannot find a good search on google.
    they have said that the ODBC-JDBC bridge driver comes with jdk1.2 or forward. and ask me to set up an ODBC data source. so i went in control panel/systems tab and then try to add SQL and then when click finish it asks me how to connect to sql server, i entered my usename and password but says connection failed.
    This is my worst nightmare. please help so i can start building web application using servlets and jsp.

    Let me tell you right now, you have BORING nightmares.

  • When to use servlets and jsp in an application like  shoping cart?

    Hi All
    I m going to design and implement a web application using servelts and jsp. I am still at its requirements analyze stage.
    The application is almost likely a shopping cart. So if any of you have the deep knowledge about this domain with servlets and jsp please help me for a good design. What I realy need to know is that
    1. what are the core requirements for a shoping cart ?
    2. use cases ?
    3. best way to follow mvc with servlets and jsp for it?
    you can answer to this at the abstract level. no need to go in detail.
    if you have any documents like design and use cases mail me to [email protected]
    your cooperation is highly appreciated in this regards.

    Look into this kid
    http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Session-Tracking.htm

  • URGENT.........File Upload using Servlets and jsp

    I am a new servlet programmer.......
    iam using tomcat server......
    can any one pls help in writing code for file upload using servlets and jsp without using multipart class or any other class like that....
    Please URGENT..

    Slow down! "Urgent" is from your perspective alone. I, myself, am not troubled or worried in the least.
    hi ugniss
    thanks for ur reply....sorry i was not
    y i was not asked not to use multipart class or any
    other class like that...is there any other
    possibility to do file uploading from jsp to
    servlet...
    Just as an aside, a JSP is a Servlet. But even if I move beyond that, the question does not make sense. If you want a "JSP to upload to a Servlet", then simply do so in memory. You are still (likely) within the same scope of a given request. However, if instead you are referring to a JSP that is displayed on a browser, then really you are talking about HTML, which is what the browser will receive. And since you are now talking about a browser, your only real, viable option is a multi-part file upload. So, it is either server or it is browser. And either way, the question leads to very established options, as outlined above.
    the main concept is.. in the browser the user selects
    a particular file and clicks the button upload..after
    clicking upload the jsp should sent the file to the
    servlet in streams...there the servlet gets in and
    saves in a server location........this is wat i hav
    to do...
    Okay. So, after reading my previous (redundant) paragraph, we have arrived at the crux of the issue. You have a JSP that will be output as HTML to a client (browser) which you want to upload content to your server (handled by a Servlet). So, you are now stuck again with multi-part. The requirement to not use multi-part is non-sensical. You can overcome it, say, if you write your own applet or standalone Swing client. However, if your users are invoking this functionality from a browser, you are limited by the options that W3C has provided you. Use multi-part.
    is there aby possibilty to do this.....can any one
    pls help....Take the advice to download and review Jakarta Commons FileUpload. Inform your management that their requirement makes no technical sense. Research on your own. There are dozens of examples (and tutorials) on file upload using multi-part. Embrace it. Live it. Love it.
    - Saish

  • How to use servlets and JSP for my homepage?

    servlets and JSP are server-side program. If my homepage is on some web-hosting websites, can I upload the servlets and JSP to run it? if possible, where can I find such a web-hosting place?
    sraiper

    Not many hosts offer JSP or Servlet support (and some do but then stop and never give you a refund, grrrrrrr!!!!).
    Also, ones that do support it can often be more expensive. There are some free ones but all the one's I've come across have problems that made them unsuitable for my needs.
    Here is a list of hosts that support Java:
    http://www.thejspbook.com/resources/category.jsp?id=1005&name=JSP+web+hosting
    I can't comment on any from that list, however I use http://www.positive-internet.com they are a bit pricey but they have excellent support and are always happy to help with any Tomcat (the JSP/Servlet server they use)configuration issues or needs that you may have.

Maybe you are looking for

  • How to duplicate a node along with its children in a JTree?

    Hello, Can anyone tell me how to duplicate/create a copy of a node along with its children? Code snippets will be appreciated. Thank you.

  • X-Path like Query option for XMLModel?

    Hi, is there an option to use x-path-like queries in the XMLModel? E.g. <?xml version="1.0" encoding="UTF-8"?>    <main>                           <config>                                      <mode>1</mode>                                       <ext

  • Pkg execution session id in all tables and Session Id of each interface

    Hi I am have one master package executing all interfaces for Staging and Target. Each staging interface loads the data from source to staging tables and target interface loads data from staging to Target. Each table has a column named ETL Batch Id an

  • Org model

    Hello Guys, I am trying to create one org structure....(testing) I am in attributes tab... I am not able to c D.channel and division..... do i need to make some setting to have them here???? there is not even a chance to selct themm.... can any1 help

  • Trying to compare concatenated items to discrete items.

    I have a reporting problem, my data is in Oracle as follows: Table A: DOCNAME                               ITEMNAME DOCUMENT1                            ITEM1 ITEM2 ITEM3 ITEM4 ITEM5 Table B DOCNAME                 ITEMNAME DOCUMENT1             ITE