Link to home
Start Free TrialLog in
Avatar of deepamyler
deepamyler

asked on

Servlet, Html & Database question.

Hi,

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.

Thanks.
Avatar of koppcha
koppcha
Flag of United States of America image

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.

Regards
Dave
Avatar of bloodredsun
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
<code>
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(){
    try{
      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
</code>

Database.jsp code
<code>
<%@ page import="java.sql.*, com.bloodredsun.beans.*" %>
<jsp:useBean id="beanconnection" class="ConnectionBean" scope="request"/>
<html>
<body>
<%
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") %>

<%
}
%>
</body>
</html>
</code>

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.

Avatar of deepamyler
deepamyler

ASKER

Hi,

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.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of bloodredsun
bloodredsun
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Glad to have helped..