Hi all,

I need a help from u... Please help me...
Is it possible to have more than 1 servlet for an J2EE application...
I have
2 JSP's (index & display page)
1 Servlet (myServlet) - Db Connectivity(insertion & selection)

In index.jsp,
i have 2 fields and a button, while pressing the button,
(1) the values of the fields have to store in the table called "user_details"
(2) And have to display into display.jsp

In display.jsp,
I have to display the user details from the user_details table and im having the profile registration fields and submit button....

(Upto this my application is working very fine)

The next step i need to do is...

While clicking the submit button in display.jsp,
(1) The profile data have to store in the another table called "register"

Now my doubt starts,

Whether we need to perform this process in separate servlet....
Can we have two servlets in one single project... Is it possible...?
whether it is needed to have a bean class for each input jsp file... Is it mandatory....?
whether i need to write my DB Connectivity in separate java class and have to import it to every java servlets...?
How can i proceed now..?
Please help me....

> Can we have two servlets in one single project... Is it possible...?

Yes.

> Is it mandatory....?

No.

Normally, a single servlet is good enough to act as a controller for your entire application. Read the JEE 5 tutorial along with this thread on the google group.

Any reason you are not using one of the many frameworks out there to ease your task? Is this college assignment / homework / practice? If not, then your really should.

Thanks for your precious reply...!

No actually i have got selected for a IT Company.... There i have to join by August 2009, so within that period i have to make up my mind and learn lot of things by practical, before entering the company....
So as per your advice, i have to use framework right...?
What are all the frameworks available....
Is Struts OK...?

Thanks for your precious reply...!

No actually i have got selected for a IT Company.... There i have to join by August 2009, so within that period i have to make up my mind and learn lot of things by practical, before entering the company....
So as per your advice, i have to use framework right...?
What are all the frameworks available....
Is Struts OK...?

If you have just joined, it would be better to dabble and understand plain JEE stuff; they anyways won't be expecting any framework expertise from you. Reading the entire JEE 5 tutorial and if possible Core JEE design patterns would be more than enough. Practice is the key here; make sure you don't end up reading things without knowing how they work.

> For the major ones there are Struts, JSF, Spring

I would rather not recommend JSF; I personally find it to be an abomination from hell. Struts2 is out BTW, so if you are picking Struts, you might as well go for Struts2. Tapestry and Wicket seem promising.

Hi all,

I started learning struts... But still i didnt worked out.
First of all, i want to know something deeply about servlets..
Its really very interesting...
But i got strucked at one thing....

Please clarify me, Hope u can...
(1) i have 2 fields(name & city) - it should be inserted to the table and have to display the table in the display.jsp- Its Working Fine. (i have used servlet, there i hv coded the DB connection & prepared statements.... in doPost method)

(2) Then i have one link "click here to register" in display.jsp - Some fields are there - that have to validate using beans, and if there are any errors, it just forward to retry.jsp with the appropriate errors msgs, or else it should be forwarded to success.jsp with the confirmation button. - Its working very fine...

(3) While click the confim button in success.jsp, that have to insert the fields into the DB....

Here i strucked... i dont know how to do....
whether i have to write code in another servlet... or in the same servlet...

If so, where i should write the code the confirm button..
In my servlet, already one doPost method is there for login button know, So what should i write....

Please anyone help me in this regard...
please solve my problem... I have struggling with that...

The codings are below...

index.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login Page</title>
        <link rel="stylesheet" type="text/css" href="css/style1.css" />
    </head>
    
    <body  bgcolor="#CCCCCC">
        <div style="background: black  ">
            <h1 align="center"><font color="#FFFFFF">Welcome to Thomas Learning Center, Singapore</font></h1>
        </div>
        
        <FORM action="http://localhost:8084/test/myServlet" method="POST">
            
            UserName<INPUT TYPE=TEXT NAME="name"><P>
            Country<INPUT TYPE=TEXT NAME="country"><P>
            
            <INPUT TYPE=SUBMIT>
        </FORM>
        
    </body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    <servlet>
        <servlet-name>myServlet</servlet-name>
        <servlet-class>classes.servlet.myServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>myServlet</servlet-name>
        <url-pattern>/myServlet</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>
</web-app>

myServlet.java

package classes.servlet;

import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;


public class myServlet extends HttpServlet {
    
    private ServletConfig config;
    //Setting JSP page
    String dispage="display.jsp";
    
    public void init(ServletConfig config)
    throws ServletException{
        this.config=config;
    }
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        
        String name = req.getParameter("name");          //getting values from jsp to servlet
        String country = req.getParameter("country");
        
        String connectionURL = "jdbc:mysql://localhost/thomas_school";
        
        Connection connection = null;
        PreparedStatement ps = null;
        
        ResultSet rs;
        int update = 0;
        List dataList = new ArrayList();
        try {
            // Load the database driver
            Class.forName("com.mysql.jdbc.Driver");
            // Get a Connection to the database
            connection = DriverManager.getConnection(connectionURL, "root", "mysql");
            //Select the data from the database
            String insert_query = "INSERT INTO details VALUES(?,?)";
            ps = connection.prepareStatement(insert_query);
            ps.setString(1,name);
            ps.setString(2,country);
            update= ps.executeUpdate();
            
            
            String list_query = "select * from details";
            Statement s = connection.createStatement();
            s.executeQuery(list_query);
            rs = s.getResultSet();
            
            while (rs.next()){
                //Add records into data list
                dataList.add(rs.getString("username"));
                dataList.add(rs.getString("Country"));
            }
            rs.close();
            s.close();
        }catch(Exception e){
            System.out.println("Exception is ;"+e);
        }
        
        req.setAttribute("data",dataList);
        //Disptching request
        RequestDispatcher dispatcher = req.getRequestDispatcher(dispage);
        if (dispatcher != null){
            dispatcher.forward(req, res);
        }
    }
}

display.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page language="java" import="java.util.*" %>

<html>
<head>
    <title>Details Display</title>
</head>
<body> 
Sucessfully inserted...!
<table border="1" width="303">
    <tr>
        <td width="119"><b>UserName</b></td>
        <td width="168"><b>Country</b></td>
    </tr>
    <%Iterator itr;%>
    <% List data= (List)request.getAttribute("data");
    for (itr=data.iterator(); itr.hasNext(); ) {
    %>
    <tr>
        <td width="119"><%=itr.next()%></td>
        <td width="168"><%=itr.next()%></td>
    </tr>
    <%}%>
</table>

<FORM >
    
    <b>Click here to Register <a href="register.jsp"></b>Create Profile</a>
    
</FORM>
</body>
</html>

register.jsp

<html>
    <body bgcolor="#CCCCCC">
        
        <div style="background: black  ">
            <h1 align="center"><font color="#FFFFFF">Thomas Learning Center - Create Your Profile Here...!</font></h1>
        </div>
        
        <form action="http://localhost:8084/test/process.jsp" method=post>
            <table align="left" cellpadding=2 cellspacing=1 border="1" bgcolor="lightblue">
                <th bgcolor="lightblue" colspan=2>
                    <font size=5>User Registration</font>
                    <br>
                    <font size=2 color="red"><sup>*</sup> Required Fields</font>
                </th>
                
                
                <tr bgcolor="lightblue">
                    <td valign=top> 
                        <b>First Name<sup>*</sup></b> 
                        <br>
                    <input type="text" name="firstName" value="" size=20 maxlength=20></td>
                    <td valign=top>
                        <b>Last Name<sup>*</sup></b>
                        <br>
                    <input type="text" name="lastName" value="" size=15 maxlength=20></td>
                </tr>
                
                
                <tr bgcolor="lightblue">
                    <td valign=top>
                        <b>E-Mail<sup>*</sup></b> 
                        <br>
                        <input type="text" name="email" value="" size=25 maxlength=125>
                    <br></td>
                    <td valign=top>
                        <b>Zip Code<sup>*</sup></b> 
                        <br>
                    <input type="text" name="zip" value="" size=10 maxlength=8></td>
                </tr>
                
                
                <tr bgcolor="lightblue">
                    <td valign=top colspan=2>
                        <b>User Name<sup>*</sup></b>
                        <br>
                        <input type="text" name="userName" size=20 value="" maxlength=10>
                    </td>
                </tr>
                <tr bgcolor="lightblue">
                    <td valign=top>
                        <b>Password<sup>*</sup></b> 
                        <br>
                    <input type="password" name="password1" size=10 value="" maxlength=10></td>
                    <td valign=top>
                        <b>Confirm Password<sup>*</sup></b>
                        <br>
                    <input type="password" name="password2" size=10 value="" maxlength=10></td>
                    <br>
                </tr>
                
                <tr bgcolor="lightblue">
                    <td valign=top>
                        <b>City<sup>*</sup></b>
                        <br>
                        <input type="text" name="city" size=20 value="" maxlength=10>
                    </td>
                    <td valign=top >
                        <b>Country<sup>*</sup></b>
                        <br>
                        <input type="text" name="country" size=20 value="" maxlength=10>
                    </td>
                </tr>
                
                
                <tr bgcolor="lightblue">
                    <td valign=top colspan=2>
                        <b>Would you like to receive e-mail notifications on our special 
                        sales?</b>
                        <br>
                        <input type="radio" name="notify" value="Yes" checked>Yes 
                        <input type="radio" name="notify" value="No" > No 
                    <br><br></td>
                </tr>
                <tr bgcolor="lightblue">
                    <td align=center colspan=2>
                        <input type="submit" value="Submit"> 
                        <input type="reset"  value="Reset">
                    </td>
                </tr>
            </table>
            
            <table align="right">
                <tr>
                    <td class="tdOne"><img src="images/admin.jpg" width="500" height="420"></td>
                    <td class="tdTwo"><img src="images/teacher.jpg" width="300" height="420"></td>
                </tr>
            </table>
        </form>
    </body>
</html>

process.jsp

<%@ page language="java" %>
<%@ page import="java.util.*" %>
<%! 
%>
<jsp:useBean id="formHandler" class="beans.FormBean" scope="request">
    <jsp:setProperty name="formHandler" property="*"/>
</jsp:useBean>
<% 
if (formHandler.validate()) {
%>
<jsp:forward page="success.jsp"/>
<%
} else {
%>
<jsp:forward page="retry.jsp"/>
<%
}
%>

retry.jsp

<jsp:useBean id="formHandler" class="beans.FormBean" scope="request"/>
<html> 
    <body  bgcolor="#CCCCCC">
        <div style="background: black  ">
            <h1 align="center"><font color="#FFFFFF">Thomas Learning Center</font></h1>
        </div>
        
        <h2 align="center">OOPS.... Error in your Data...</h2>
        
        <form action="http://localhost:8084/test/process.jsp" method=post>
            <center>
                <table cellpadding=4 cellspacing=2 border=0>
                    <th bgcolor="lightblue" colspan=2>
                        <font size=5>User Registration</font>
                        <br>
                        <font size=2 color="red"><sup>*</sup> Required Fields </font>
                    </th>
                    <tr bgcolor="lightblue">
                        <td valign=top> 
                            <B>First Name<sup>*</sup></B> 
                            <br>
                            <input type="text" name="firstName" 
                                   value='<%=formHandler.getFirstName()%>' size=15 maxlength=20>
                            <br><font size=2 
                                      color=red><%=formHandler.getErrorMsg("firstName")%></font>
                        </td>
                        <td valign=top>
                            <B>Last Name<sup>*</sup></B>
                            <br>
                            <input type="text" name="lastName" 
                                   value='<%=formHandler.getLastName()%>' size=15 maxlength=20>
                            <br><font size=2 
                                      color=red><%=formHandler.getErrorMsg("lastName")%></font>
                        </td>
                    </tr>
                    <tr bgcolor="lightblue">
                        <td valign=top>
                            <B>E-Mail<sup>*</sup></B> 
                            <br>
                            <input type="text" name="email" value='<%=formHandler.getEmail()%>' 
                                   size=25 maxlength=125>
                            <br><font size=2 color=red><%=formHandler.getErrorMsg("email")%></font>
                        </td>
                        <td valign=top>
                            <B>Zip Code<sup>*</sup></B> 
                            <br>
                            <input type="text" name="zip" value='<%=formHandler.getZip()%>' size=5 
                                   maxlength=6>
                            <br><font size=2 color=red><%=formHandler.getErrorMsg("zip")%></font>
                        </td>
                    </tr>
                    <tr bgcolor="lightblue">
                        <td valign=top colspan=2> 
                            <B>User Name<sup>*</sup></B>
                            <br>
                            <input type="text" name="userName" size=10 
                                   value='<%=formHandler.getUserName()%>' maxlength=10>
                            <br><font size=2 
                                      color=red><%=formHandler.getErrorMsg("userName")%></font>
                        </td>
                    </tr>
                    <tr bgcolor="lightblue">
                        <td valign=top>
                            <B>Password<sup>*</sup></B> 
                            <br>
                            <input type="password" name="password1" size=10 
                                   value='<%=formHandler.getPassword1()%>' maxlength=10>
                            <br><font size=2 
                                      color=red><%=formHandler.getErrorMsg("password1")%></font>
                        </td>
                        <td valign=top>
                            <B>Confirm Password<sup>*</sup></B>
                            <br>
                            <input type="password" name="password2" size=10 
                                   value='<%=formHandler.getPassword2()%>' maxlength=10>
                            <br><font size=2 
                                      color=red><%=formHandler.getErrorMsg("password2")%></font>
                        </td>
                        <br>
                    </tr>
                    <tr bgcolor="lightblue">
                        <td valign=top> 
                            <B>City<sup>*</sup></B>
                            <br>
                            <input type="text" name="city" size=10 
                                   value='<%=formHandler.getCity()%>' maxlength=10>
                            <br><font size=2 
                                      color=red><%=formHandler.getErrorMsg("city")%></font>
                        </td>
                        <td valign=top> 
                            <B>Country<sup>*</sup></B>
                            <br>
                            <input type="text" name="country" size=10 
                                   value='<%=formHandler.getCountry()%>' maxlength=10>
                            <br><font size=2 
                                      color=red><%=formHandler.getErrorMsg("country")%></font>
                        </td>
                    </tr>
                    
                    
                    
                    <tr bgcolor="lightblue">
                        <td colspan=2 valign=top>
                            <B>Would you like to receive e-mail notifications on our special 
                            sales?</B>
                            <br>
                            <input type="radio" name="notify" value="Yes" 
                                   <%=formHandler.isRbSelected("Yes")%>>Yes 
                            <input type="radio" name="notify" value="No" 
                                   <%=formHandler.isRbSelected("No")%>> No 
                        <br><br></td>
                    </tr>
                    <tr bgcolor="lightblue">
                        <td colspan=2 align=center>
                            <input type="submit" value="Submit"> <input type="reset"  value="Reset">
                        </td>
                    </tr>
                </table>
            </center>
        </form>
    </body>
</html>

success.jsp

<jsp:useBean id="formHandler" class="beans.FormBean" scope="request"/>
<html>
    <body  bgcolor="#CCCCCC">
        <div style="background: black  ">
            <h1 align="center"><font color="#FFFFFF">Thomas Learning Center - Please confirm your Details</font></h1>
        </div>
        
        <form action="http://localhost:8084/test/confirm.jsp" method=post>
            <center>
                <table cellpadding=1 cellspacing=1 border="1" >
                    <th bgcolor="lightblue" colspan=2>
                        <font size=5>You are going to create ur profile as</font>
                    </th>
                    
                    
                    <font size=4>
                        <tr bgcolor="lightblue">
                            <td valign=top> 
                                <b>First Name</b> 
                                <br>
                                <jsp:getProperty name="formHandler" property="firstName"/>
                            </td>
                            <td valign=top>
                                <b>Last Name</b>
                                <br>
                                <jsp:getProperty name="formHandler" property="lastName"/>
                            </td>
                        </tr>
                        <tr bgcolor="lightblue">
                            <td valign=top>
                                <b>E-Mail</b> 
                                <br>
                                <jsp:getProperty name="formHandler" property="email"/>
                            <br></td>
                            <td valign=top>
                                <b>Zip Code</b> 
                                <br>
                                <jsp:getProperty name="formHandler" property="zip"/>
                            </td>
                        </tr>
                        
                        
                        
                        
                        <tr bgcolor="lightblue">
                            <td valign=top colspan=2>
                                <b>User Name</b>
                                <br>
                                <jsp:getProperty name="formHandler" property="userName"/>
                            </td>
                        </tr>
                        
                        <tr bgcolor="lightblue">
                            <td valign=top>
                                <b>City</b>
                                <br>
                                <jsp:getProperty name="formHandler" property="city"/>
                            </td>
                            <td valign=top>
                                <b>Country</b>
                                <br>
                                <jsp:getProperty name="formHandler" property="country"/>
                            </td>
                        </tr>
                        
                        <tr bgcolor="lightblue">
                            <td colspan=2 valign=top>
                                <b>Would you like to receive e-mail notifications on our special 
                                sales?</b>
                                <br>
                                <jsp:getProperty name="formHandler" property="notify"/>
                            </td>
                        </tr>
                    </font>
                </table>
                <br>
                <input type="submit" value="Confirm"> 
                
            </center>
        </form>
    </body>
</html>

confirm.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
--%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Success</title>
        <link rel="stylesheet" type="text/css" href="css/style1.css" />
    </head>
    <body bgcolor="#CCCCCC">
        
        <h1>U have a successfully created ur profile...!!!</h1>    
        
        
    </body>
</html>

Please help me...
If i want to write seperate DAO class, what all are the code should be in DAO class... (from Loading driver to what...???)
Where should i invoke this DAO class....????
Please clarify this....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.