Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

Servlet, Html & Database question.


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.

1 Solution
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?
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.

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.

ConnectionBean.java 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 ) ;
}//end ConnectionBean.java

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 ( rs.next() ){
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.

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

deepamylerAuthor Commented:

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.

>>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="servlet.do?page=index.jsp&userId=1234">Go to index</a>
<a href="servlet.do?page=personal.jsp&userId=1234">Go to personal</a>
<a href="servlet.do?page=checkout.jsp&userId=1234">Go to checkout</a>
Glad to have helped..

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now