Servlet, Html & Database question.

Posted on 2005-04-15
Last Modified: 2010-04-01

I have  a question regarding a web application using Database.
I want to create a webapplication using HTML & servlets. Whenever the user enters the id on the web application. It should validate with the Database tables which I have already created. And if the id  of the user is correct then the other information entered by the user should be stored in the database.
How to write the code to do all these features?
Kindly help.

Question by:deepamyler
    LVL 8

    Expert Comment

    You can certainly do that if you  be more clear.

    In your table in the database do you have the table with just the user Id and all other columns being null?If so

    Format will go like this
    1>Get the Ids and store it in a bean
    2>Create a jsp and use the bean in the JSP
    3>When ever user enters a id check that with the values in the bean
    4>If it is successfult then run a update query on the database using JDBC

    We would be able to help you more if you clearly explain what how your design is?
    LVL 16

    Expert Comment

    Hi deepamyler,

    Have you prepared all the environment ready? I mean that do you set everything for JSP/HTML - Servlet - Database ready? And the Servlet can connect to database already.

    If so, you can easily get the id from the user's input....normally from getParameter() in your Servlet. Then you need to query your database whether this value is or is NOT in the database. If yes, you may redirect him or her to success.html otherwise bad.html.

    Please let me know if you need any help.

    LVL 29

    Expert Comment

    Here's a load of code from an earlier question that I answered. Most of it would be applicable to your situation.

    All the points made above are valid, you need to post a lot more information as regards to your db schema, table names, how your servlet container is configured etc, and the general advice with regards to the flow of the application is good. You should use a form to get the username and password to send to a jsp or servlet which then does the authentication. code // our javabean
    package com.bloodredsun.beans ;
    import java.sql.*;

    public class Connectionbean{

    private Connection conn ;
    private Statement stmt ;
    private static final String driver = "" ; // your db driver
    private static final String dbURL= "" ; //your db url
    private static final String login= "" ; // your db login
    private static final String password= "" ; //your db password

      public ConnectionBean(){
          Class.forName( driver ) ;
          conn = DriverManager.getConnection(dbURL, login, password);
          stmt = conn.createStatement() ;

        }catch (Exception e){
          System.out.print("Whoops, buggered") ;
      }//end ConnectionBbean constructor

          public ResultSet executeQuery ( String pSql){
          return stmt.executeQuery( pSql ) ;

    Database.jsp code
    <%@ page import="java.sql.*, com.bloodredsun.beans.*" %>
    <jsp:useBean id="beanconnection" class="ConnectionBean" scope="request"/>
    String sql = "Select * from myTable" ;
    ResultSet rs = beanconnection.executeQuery( sql) ;
    <%-- We now have the ResultSet, do what you will --%>
    while ( ){
    A line of db stuff <%= rs.getString("id") %>


    This is bare-arsed stuff, you will have to complete it with the normal extra methods in the bean for closing the connection, and other things like that and therer is minimal exception handling (in fact I've not tested this but written it straight out, but the theory is sound). What you can see is that Javabeans allow you to COMPONENTIZE code in that the code required for creating the db connection and statement were put in the JavaBean. This can then be dropped in as many pages as you like with out the need for repeatedly setting up the connection.

    Once you have got used to JavaBeans in your JSP, I would recommend then doing it in a servlet. Then, once you've done that, have a look at custom tags and you'll see why JSTL is so popular (JSTL just being a set of standardised custom tags).

    My next advice would be to look at dataSources and connection pools, this has a huge performance benefit for larger projects and also takes you into the world of JNDI (which isn't a frightening as it seems!).

    Good luck and happy coding.


    Author Comment


    I have created the servlet for the validation of the user info. After the validation the user is taken to the main information page in which there are several links to the various online forms. Based upon the link the user clicks he will be taken to appropriate servlet or jsp page. If he has filled the form once he will not be allowed to  fill the form again but given the summary of the form information( a JSP page) he had filled in.
    My question is if I  want to use href for the links instead of radio buttons  how will I know the name - value of the link selected to proceed further.
    I don't want to use radio buttons for the links.

    LVL 29

    Accepted Solution

    >>My question is if I  want to use href for the links instead of radio buttons  how will I know the name - value of the link selected to proceed further.

    Use request parameters for the links. You can have as many as you like and there are always "name" and "value", joined with an equals sign (=)

    <a href="">Go to index</a>
    <a href="">Go to personal</a>
    <a href="">Go to checkout</a>
    LVL 29

    Expert Comment

    Glad to have helped..

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now