0% found this document useful (0 votes)
18 views

Servlet Programs Manual 21-24

Uploaded by

sarthi1505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Servlet Programs Manual 21-24

Uploaded by

sarthi1505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Installation of Apache Tomcat webserver.

Source:
Download Apache Tomcat Application from Apache Tomcat website
Run the application file.

STEP 1: Simply click on the „Next„button to continue installation process.

STEP 2: Accept the terms of the agreement by clicking on „I Agree„button.

III B.Tech I Sem | Department of Computer Science & Engineering 97


STEP 3:
Choose the features of Apache Tomcat you want to install by checking the components and
click „Next„.
STEP 4:
 The default port number for Tomcat to process HTTP requests is 8080. You can either
change the port number here or change it later after the installation in server.xml which
is located in /conf/server.xml. We recommend to have the default value.
 You can provide the username and password for Administrator login but here, we leave
it as blank.
 Now, click on the „Next„ button.

III B.Tech I Sem | Department of Computer Science & Engineering 99


STEP 5:
 The installer uses the registry to determine the base path of a Java 5 or later JRE,
including the JRE installed as part of the full JDK.
 When running on a 64-bit operating system, the installer will first look for a 64-bit JRE
and only look for a 32-bit JRE if a 64-bit JRE is not found.
 It is not mandatory to use the default JRE detected by the installer. Any installed Java 5
or later JRE (32-bit or 64-bit) may be used by clicking on the browse button and click
„Next„.

III B.Tech I Sem | Department of Computer Science & Engineering 100


STEP 6:
In Windows, by default the location will be provided as „C:\Program Files\Apache
Software Foundation\Tomcat 6.0′. But for simplicity, we recommend you to use
„C:\Tomcat6„ as shown below and click „Install„.
STEP 7: Installing window is displayed.

STEP 8:
Tomcat setup completion window is displayed as shown below and click on „Finish„
button.

STEP 9:
Apache Service Manager window appears which is attempting to start the Tomcat service.

STEP 10:

Open browser and type http://localhost:8080. You should see the Apache Tomcat home page as
shown below.
EXP 21- Write a Java Program to create a simple Servlet and test it on tomcat server?

FirstServlet.java:
import java.io.*;
import javax.servlet.*;
public class FirstServlet extends GenericServlet
{
public void service(ServletRequest req,ServletResponse res)throws
ServletException,IOException{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("<html><head><title>First Servlet</title></head>");
pw.println("<body><center><h1>This Message came from a servlet</h1>");
pw.println("</center></body></html>");
pw.close();
}
}
Web.xml:
<web-app>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
Output
EXPERIMENT 22 Write a Java Program to create a Servlet that Reads Request Parameter(s) and
Display their value(s)?

<html>
<head>
<title>Servlet Parameters</title>
</head>
<body>
<center>
<form name="form1" method="POST" action="http://localhost:9401/rpar/rparam">
<table>
<tr>
<td><b>Employee</td>
<td><input type="text" name="ename" value=""></td>
</tr>
<tr>
<td><b>Id</td>
<td><input type="text" name="id" value=""></td>
</tr>
<tr>
<td><input type="submit" value="submit"></td>
<td><input type="reset" value="clear"></td>
</tr>
</table>
</form>
</center>
</body>
</html>

Rparam.java

import java.io.*;
import java.util.*;
import javax.servlet.*;
public class Rparam extends GenericServlet
{
public void service(ServletRequest req,ServletResponse res)throws
ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
Enumeration e=req.getParameterNames();
while(e.hasMoreElements()){
String pname=(String)e.nextElement();
pw.println("<b>"+pname+"=");
String pvalue=req.getParameter(pname);
pw.println("<b>"+pvalue);
}
pw.close();
}
}
Web.xml:
<web-app>
<servlet>
<servlet-name>rp</servlet-name>
<servlet-class>Rparam</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>rp</servlet-name>
<url-pattern>/rparam</url-pattern>
</servlet-mapping>
</web-app>

OutPut:
Experiment 24: Write a program to implement Session tracking using Cookies.

index.html

1. <form action="servlet1" method="post">


2. Name:<input type="text" name="userName"/><br/>
3. <input type="submit" value="go"/>
4. </form>
FirstServlet.java

1. import java.io.*;
2. import javax.servlet.*;
3. import javax.servlet.http.*;
4.
5.
6. public class FirstServlet extends HttpServlet {
7.
8. public void doPost(HttpServletRequest request, HttpServletResponse response){
9. try{
10.
11. response.setContentType("text/html");
12. PrintWriter out = response.getWriter();
13.
14. String n=request.getParameter("userName");
15. out.print("Welcome "+n);
16.
17. Cookie ck=new Cookie("uname",n);//creating cookie object
18. response.addCookie(ck);//adding cookie in the response
19.
20. //creating submit button
21. out.print("<form action='servlet2'>");
22. out.print("<input type='submit' value='go'>");
23. out.print("</form>");
24.
25. out.close();
26.
27. }catch(Exception e){System.out.println(e);}
28. }
29. }

SecondServlet.java

1. import java.io.*;
2. import javax.servlet.*;
3. import javax.servlet.http.*;
4.
5. public class SecondServlet extends HttpServlet {
6.
7. public void doPost(HttpServletRequest request, HttpServletResponse response){
8. try{
9.
10. response.setContentType("text/html");
11. PrintWriter out = response.getWriter();
12.
13. Cookie ck[]=request.getCookies();
14. out.print("Hello "+ck[0].getValue());
15.
16. out.close();
17.
18. }catch(Exception e){System.out.println(e);}
19. }
20.
21.
22. }
web.xml
1. <web-app>
2.
3. <servlet>
4. <servlet-name>s1</servlet-name>
5. <servlet-class>FirstServlet</servlet-class>
6. </servlet>
7.
8. <servlet-mapping>
9. <servlet-name>s1</servlet-name>
10. <url-pattern>/servlet1</url-pattern>
11. </servlet-mapping>
12.
13. <servlet>
14. <servlet-name>s2</servlet-name>
15. <servlet-class>SecondServlet</servlet-class>
16. </servlet>
17.
18. <servlet-mapping>
19. <servlet-name>s2</servlet-name>
20. <url-pattern>/servlet2</url-pattern>
21. </servlet-mapping>
22.
23. </web-app>
Output

You might also like