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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

Register Servlet Example Error

Hi,

I was trying to run following example. At login page if the user comes for the first time, user supposed to click Register user where he supposed to ener first name, last name, address etc details to register. Once you click submit it supposed to go to RegistrationSevlet which should simply forward to Shot.html.

Problem I am facing is when I click the Register link in login page it is successfully going to Register.html page where I am filling all the details and submiting not leading to to Shot.html through RegistrationServlet.java. Please advise.
Hi,

I was trying to run following example. At login page if the user comes for the first time, user supposed to click Register user where he supposed to ener first name, last name, address etc details to register. Once you click submit it supposed to go to RegistrationSevlet which should simply forward to Shot.html.

Problem I am facing is when I click the Register link in login page it is successfully going to Register.html page where I am filling all the details and submiting not leading to to Shot.html through RegistrationServlet.java. Please advise.


register.html

<HTML>
<BODY>

<P align="center"><FONT size="6" color="#008040" face="Verdana"> Welcome
to BuyForLess </FONT></P>
<P align="center"><BR />
Please fill in the following details for registration</P>
<DIV align="center">
<hr />
<form action="RegistrationServlet">
<TABLE border="0">
<TBODY>
<TR>
<TD width="142">Name</TD>
<TD width="222"><INPUT name="name" size="35" /></TD>
</TR>
<TR>
<TD width="142">Email</TD>
<TD width="222"><INPUT name="email" size="35" /></TD>
</TR>
<TR>
<TD width="142">Address Line 1</TD>
<TD width="222"><INPUT name="addressline1" size="35"
/></TD>
</TR>
<TR>
<TD width="142">City</TD>
<TD width="222"><INPUT name="city" size="35" /></TD>
</TR>
<TR>
<TD width="142">State</TD>
<TD width="222"><INPUT name="state" size="35" /></TD>
</TR>
<TR>
<TD width="142">Country</TD>
<TD width="222"><INPUT name="country" size="35" /></TD>
</TR>
<TR>
<TD width="142">UserName</TD>
<TD width="222"><INPUT name="username" size="35" /></TD>
</TR>
<TR>
<TD width="142">Password</TD>
<TD width="222"><INPUT name="password" size="35" /></TD>
</TR>
<TR>
<TD width="142"></TD>
<TD width="222"><INPUT type="submit" name="submit"
value="Register" />
</TD>
</TR>
</TBODY>
</TABLE>
</form>
</DIV>
<hr />
</BODY>
</HTML>

RegistrationServlet.java



import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class RegistrationServlet extends HttpServlet {
      protected void doGet(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
            // Read the data
            String name = req.getParameter("name");
            String email = req.getParameter("email");
            String line1 = req.getParameter("addressline1");
            String city = req.getParameter("city");
            String state = req.getParameter("state");
            String country = req.getParameter("country");
            String userid = req.getParameter("username");
            String password = req.getParameter("password");
            insertToDatabase(name, email, line1, city, state,
                        country, userid, password);
            // Forward to Shopping page
            //req.getRequestDispatcher("/htmls/shop.html").forward(req, res);
            RequestDispatcher rd1 = req.getRequestDispatcher("/htmls/shop.html");
            rd1.forward(req, res);
      }
      private void insertToDatabase(String name, String email, String line1,
                  String city, String state, String country,
                  String userid, String password) {
            // Write JDBC logic to insert data to REGCUSTOMERS table
      }
}


web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
  <servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>HelloWorld</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/HelloServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>Hello1</servlet-name>
    <servlet-class>LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Hello1</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>Order</servlet-name>
    <servlet-class>OrderProcessingServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Order</servlet-name>
    <url-pattern>/OrderProcessingServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>billing</servlet-name>
    <servlet-class>BillingServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>billing</servlet-name>
    <url-pattern>/BillingServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>Confirmation</servlet-name>
    <servlet-class>ConfirmationServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Confirmation</servlet-name>
    <url-pattern>/ConfirmationServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>Banner</servlet-name>
    <servlet-class>BannerServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Banner</servlet-name>
    <url-pattern>/BannerServlet</url-pattern>
  </servlet-mapping>
    <servlet>
    <servlet-name>Register</servlet-name>
    <servlet-class>RegistrationServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Register</servlet-name>
    <url-pattern>/RegistrationServlet</url-pattern>
  </servlet-mapping>
</web-app>


Login.html


<HTML>
<BODY>
      <P align="center">
            <FONT size="6" color="#008040" face="Verdana">Welcome to
                  BuyForLess</FONT>
      </P>
      <P>
            <BR>
      </P>
      <DIV align="center">
            <form action="LoginServlet">
                  <TABLE border="0">
                        <TBODY>
                              <TR>
                                    <TD width="109"><FONT size="3" face="verdana">Login ID</FONT>
                                    </TD>
                                    <TD width="255"><INPUT type="text" name="userid" size="35">
                                    </TD>
                              </TR>
                              <TR>
                                    <TD width="109"><FONT size="3" face="verdana">Password</FONT>
                                    </TD>
                                    <TD width="255"><INPUT type="password" name="password"
                                          size="35">
                                    </TD>
                              </TR>
                              <TR>
                                    <TD width="109"></TD>
                                    <TD width="255"><INPUT type="submit" name="submit"
                                          value="Login">
                                    </TD>
                              </TR>
                        </TBODY>
                  </TABLE>
                  </form>
                  <BR> <FONT size="3" face="verdana">If you are visiting
                        for the first time, please <A href="htmls/register.html">register
                              here</A>
                  </FONT>
      </DIV>
</BODY>
</HTML>




Shop.html





<html>
<head>
<title>Customer Form</title>
</head>
<body>
      <P align="center">
            <FONT size="6" color="#008040" face="Verdana"> Welcome to
                  BuyForLess </FONT>
      </P>
      <hr />
      <h4 align="center">
            <FONT size="3" face="verdana"> Please Select the following
                  items and click Buy to purchase </FONT>
      </h4>
      <form action="OrderProcessingServlet">
            <table border="1" cellspacing="1" cellpadding="1">
                  <tr>
                        <td align="center">Book Name</td>
                        <td align="center">Distributer</td>
                        <td align="center">Price</td>
                        <td align="center">Quantity</td>
                  </tr>
                  <tr>
                        <td align="center">Advanced J2EE</td>
                        <td align="center">Hall Publications</td>
                        <td align="center">$10.99</td>
                        <td align="center">Chapter 13 Request Dispatching Servlet
                              Programming 368 <SELECT name="j2eeQty">
                                    <OPTION value="0">0</OPTION>
                                    <OPTION value="1">1</OPTION>
                                    <OPTION value="2">2</OPTION>
                                    <OPTION value="3">3</OPTION>
                        </SELECT></td>
                  </tr>
                  <tr>
                        <td align="center">Perl Scripting</td>
                        <td align="center">Wrox Publications</td>
                        <td align="center">$9.99</td>
                        <td align="center"><SELECT name="perlQty">
                                    <OPTION value="0">0</OPTION>
                                    <OPTION value="1">1</OPTION>
                                    <OPTION value="2">2</OPTION>
                                    <OPTION value="3">3</OPTION>
                        </SELECT></td>
                  </tr>
                  <tr>
                        <td align="center">Red Roses</td>
                        <td align="center">CoolFlowers.com</td>
                        <td align="center">$1.99</td>
                        <td align="center"><SELECT name="flowerQty">
                                    <OPTION value="0">0</OPTION>
                                    <OPTION value="1">1</OPTION>
                                    <OPTION value="2">2</OPTION>
                                    <OPTION value="3">3</OPTION>
                        </SELECT></td>
                  </tr>
                  <tr>
                        <td></td>
                        <td align="center"><input type="submit" value="Checkout"
                              size="20" /></td>
                  </tr>
            </table>
      </form>
      <br />
      <i>Copyright 2001 BuyForLess.com</i>
</body>
</html>


0
gudii9
Asked:
gudii9
  • 5
  • 3
1 Solution
 
CEHJCommented:
What did you see when you checked the log file?
0
 
gudii9Author Commented:
Looks like some path error. But I was not able to figure out why it is going to wrong path.

going to
/ServletExample/htmls/RegistrationServlet path instead of
/ServletExample/RegistrationServlet.

I checked WEB.XML as well and the registration.html(<form action="RegistrationServlet">) and Registrationservlet(RequestDispatcher rd1 = req.getRequestDispatcher("/htmls/shop.html");). All looks goo to me. Please advise
RegisterError.JPG
0
 
CEHJCommented:
<form action="RegistrationServlet">

Open in new window


should probably be

<form action="../RegistrationServlet">

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
gudii9Author Commented:
With <form action="../RegistrationServlet">
 worked perfect. How it worked with .. at front.

Other similar HTMLs like Billing and shopping worked without ../ at front in the form action. Please advise
Billing.html

<HTML>
<BODY>
<P align="center"><FONT size="6" color="#008040" face="Verdana">Welcome
to BuyForLess</FONT></P>
<P><BR>
</P>
<DIV align="center">
<hr />
<form action="BillingServlet">
<TABLE border="0">
<TBODY>
<TR>
<TD width="225">Card Type</TD>
<TD width="347"><INPUT type="radio" name="cardtype"
value="MasterCard" checked> MasterCard
<INPUT type="radio" name="cardtype" value="Visa">
VISA
</TD>
</TR>
<TR>
<TD width="225">Credit Card Number</TD>
<TD width="347">
<INPUT type="text" name="number" size="35"></TD>
</TR>
<TR>

<TD width="225">Expiration Date</TD>
<TD width="347"><INPUT name="date" size="15"></TD>
</TR>
<TR>
<TD width="225"></TD>
<TD width="347">
<INPUT type="submit" name="submit" value="Submit"></TD>
</TR>
</TBODY>
</TABLE>
</form>
</BODY>
<hr />

</HTML>

shop.html

<html>
<head>
<title>Customer Form</title>
</head>
<body>
	<P align="center">
		<FONT size="6" color="#008040" face="Verdana"> Welcome to
			BuyForLess </FONT>
	</P>
	<hr />
	<h4 align="center">
		<FONT size="3" face="verdana"> Please Select the following
			items and click Buy to purchase </FONT>
	</h4>
	<form action="OrderProcessingServlet">
		<table border="1" cellspacing="1" cellpadding="1">
			<tr>
				<td align="center">Book Name</td>
				<td align="center">Distributer</td>
				<td align="center">Price</td>
				<td align="center">Quantity</td>
			</tr>
			<tr>
				<td align="center">Advanced J2EE</td>
				<td align="center">Hall Publications</td>
				<td align="center">$10.99</td>
				<td align="center">Chapter 13 Request Dispatching Servlet
					Programming 368 <SELECT name="j2eeQty">
						<OPTION value="0">0</OPTION>
						<OPTION value="1">1</OPTION>
						<OPTION value="2">2</OPTION>
						<OPTION value="3">3</OPTION>
				</SELECT></td>
			</tr>
			<tr>
				<td align="center">Perl Scripting</td>
				<td align="center">Wrox Publications</td>
				<td align="center">$9.99</td>
				<td align="center"><SELECT name="perlQty">
						<OPTION value="0">0</OPTION>
						<OPTION value="1">1</OPTION>
						<OPTION value="2">2</OPTION>
						<OPTION value="3">3</OPTION>
				</SELECT></td>
			</tr>
			<tr>
				<td align="center">Red Roses</td>
				<td align="center">CoolFlowers.com</td>
				<td align="center">$1.99</td>
				<td align="center"><SELECT name="flowerQty">
						<OPTION value="0">0</OPTION>
						<OPTION value="1">1</OPTION>
						<OPTION value="2">2</OPTION>
						<OPTION value="3">3</OPTION>
				</SELECT></td>
			</tr>
			<tr>
				<td></td>
				<td align="center"><input type="submit" value="Checkout"
					size="20" /></td>
			</tr>
		</table>
	</form>
	<br />
	<i>Copyright 2001 BuyForLess.com</i>
</body>
</html>

Open in new window

0
 
CEHJCommented:
With <form action="../RegistrationServlet">
 worked perfect. How it worked with .. at front.

Other similar HTMLs like Billing and shopping worked without ../ at front in the form action. Please advise

Look at your directory structure. Your html code is in a directory off the web root. Your servlet(s) operate at the web root. If you're calling code html to html then the depth is the same. Between html and servlets, it's different.
0
 
gudii9Author Commented:
Billing.html (<form action="BillingServlet">) and Shop.html(<form action="OrderProcessingServlet">) both are also calling Servlets at a directory off the web root but still they are working without ../ which reaclly surprises me. Also in some servlets directory structure for calling is like
RequestDispatcher rd1 = req.getRequestDispatcher("/htmls/shop.html");

I am confused with .. or / or nothing at front for giving the path. please advise
0
 
CEHJCommented:
Not sure why those are working in the circumstances you mention
0
 
CEHJCommented:
:)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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