Create a User Interface (Calculator.html)
|
-
<HTML>
- <TITLE>ONLINE SHOPPING PORTAL</TITLE>
- <BODY>
<FORM ACTION = "http://localhost:8080/abc5/abc" METHOD = GET align=CENTER>
Enter First Number: <INPUT TYPE = TEXT NAME = "num1" align=CENTER><BR>
Enter Second Number: <INPUT TYPE = TEXT NAME = "num2" align=CENTER><BR>
<INPUT TYPE = SUBMIT VALUE = "ADD" align=CENTER>
</FORM>
</BODY>
- </HTML>
Download Calculator.html
|
Create a Servlet
|
-
import javax.servlet.*;
-
import javax.servlet.http.*;
-
import java.io. *;
public class Calculate extends HttpServlet-
{
-
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
-
{
-
PrintWriter pw=res.getWriter();
-
int number1=Integer.parseInt(req.getParameter("num1"));
-
int number2=Integer.parseInt(req.getParameter("num2"));
-
int sum=number1+number2;
-
pw.println("Sum of the numbers is "+sum);
-
}
-
}
Download Calculate.java
|
Create a Customized Error Page
|
-
import javax.servlet.*;
-
import javax.servlet.http.*;
-
import java.io.*;
public class ErrorServlet extends HttpServlet-
{
-
final String EXC = "javax.servlet.error.exception";
-
final String MSG = "javax.servlet.error.message";
-
final String ST = "javax.servlet.error.status_code";
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException-
{
-
ServletContext sc = getServletContext();
-
PrintWriter pw = response.getWriter();
-
Exception exc = (Exception)request.getAttribute(EXC);
-
Integer st_cd = (Integer)request.getAttribute(ST);
-
String msg = (String)request.getAttribute(MSG);
-
pw.println("<HTML>");
-
pw.println("<BODY>");
-
pw.println("<HR>");
-
pw.println("<H1>Sorry, an error has occurred that has prevented the server from servicing your request.</H1>");
-
pw.println("<FONT SIZE = 5>");
-
pw.println("<TABLE ALIGN = CENTER>");
-
pw.println("<TR BGCOLOR = LIGHTGREY>");
-
pw.println("<TD><B> Status Code : </B></TD><TD>" + st_cd + " </TD>");
-
pw.println("</TR>");
-
pw.println("<TR>");
-
pw.println("<TD><B> Type of Exception :</B></TD><TD>" + exc.getClass() + " </TD>");
-
pw.println("</TR>");
-
pw.println("<TR BGCOLOR = LIGHTGREY>");
-
pw.println("<TD><B> Message Description : </B></TD><TD>" + msg + " </TD><HR/>");
-
String str=exc.toString()+st_cd.toString()+msg;
-
sc.log("Exception occurred", exc);
-
pw.println("</TR>");
-
pw.println("</TABLE>");
-
pw.println("</FONT>");
-
pw.println("<HR>");
-
pw.println("<HR>");
-
pw.println("<CENTER><H1>Please try again...</H1></CENTER>");
-
pw.println("</BODY>");
-
pw.println("</HTML>");
-
}
-
}
Download ErrorServlet.java
|
Map the Error Page to the Web Application
|
-
Goto Start->Programs->Sun Microsystems->Application Server PE->Start Default Server
-
Goto Start->Programs-> Sun Microsystems->Application Server PE->Deploytool
- Goto File ->New -> Application Note: Inserted of EmployeeDetails use Caluclate
 (Click the Browse button)
-
 (Select the folder in the Look In dropdown box, and then give a file name “Caluclate”. Next click the New Application button)
 (Click the OK button)
- Now goto File -> Save to save the file
- Next, goto File -> New -> Web Component
 (Click Next button)
 (Enter the WAR Name as “EmpApp” and then click the Edit Contents… button)
 (Select all the .class, .jsp , .tld and .html files and click the Add button)
 (Now click the OK button)
 (Now click the Next button)
 (Now select the Servlet option button and then click the Next button)
 (Now select the “Caluclate” from the Servlet Class dropdown box)
 (Now select the Next button)
 (Now select the Finish button)
- Next, goto File -> New -> Web Component
 (Click Next button)
 (Here, select the option Add to Existing WAR Module and select WebApp (ErrorDemo) in the WAR Location) (Note: WebApp is the Calculate servlets WAR Display Name and ErrorDemo is the Application Name (ie. We first created File->New->Application))
 (Now click the Next button)
 (Now select the Servlet option button and then click the Next button)
 (Now select the “ErrorServlet” from the Servlet Class dropdown box)
 (Now select the Next button)
 (Now select the Finish button)
 (Now select the EmpApp in the left pane and select the General tab in the right pane. Here give a name “example4” in the Context Root text box)
- Next select the Calculate in the right side
 (Now select the Calculate in the left pane and then select the Aliases tab in the right pane. Next select the Add button)
 (Now add a name as “Calculate”)

- Now select WebApp in the left pan and then select the File Refs tab in the right pane

- Now click the Add Error button in the Error Mapping section of the File Refs tab. Then type the exception in the Error/Exception textbox and the name of the error page in the Resource to be Called textbox. In this example use ErrorServlet-Alias-name to handle java.lang.NumberFormatException

- Now goto File ->Save
- Next goto Tools -> Deployee
 (Enter the User Name as “admin” and Password as “password”. Next click the OK button)
 (Now a message --- Operation Completed Successfully --- must display. Next click the Close button)
- Next goto File -> Exit to close it
- Now open an Internet Explorer and type the address http:// localhost:8080/example4/Calculate/Calculator.html

- Enter two values and click the add button

- Now enter a value in one text and a character in another text

- Program completed Successfully
|
Click for Next Topic
|
|
|
|
About Author
|
Author Name : Mr. R.Franklin Bourgia Singh
Country : INDIA
Working In : Aryans Infoway (P) Ltd.
Email : admin@completetutorials.co.cc
Author Home Page : http://www.completetutorials.googlepages.com
|
Reach me!
|
|
Send your feed-back and query to admin@completetutorials.co.cc
©2008 Bourgia
|