0% found this document useful (0 votes)
7 views15 pages

Servlet Examples and Execution Guide

This document provides examples of servlet applications in Java, including how to set up a web.xml configuration file, handle form data, and interact with a database. It covers various servlet functionalities such as printing messages, accepting form data, managing sessions, and retrieving data from a database. Each example includes the necessary Java code, HTML files, and execution procedures for deploying the servlets on a server like Tomcat.

Uploaded by

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

Servlet Examples and Execution Guide

This document provides examples of servlet applications in Java, including how to set up a web.xml configuration file, handle form data, and interact with a database. It covers various servlet functionalities such as printing messages, accepting form data, managing sessions, and retrieving data from a database. Each example includes the necessary Java code, HTML files, and execution procedures for deploying the servlets on a server like Tomcat.

Uploaded by

vijaya51good
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT- 4 (SERVLETS)

Examples:

[Link] application to Print a message.

[Link]

<?xml version="1.0" encoding="ISO-8859-1"?>


<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"[Link]
<web-app>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
</web-app>

[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Hello Servlet!</title>");
[Link]("</head>");
[Link]("<body bgcolor=dodgerblue>");
[Link]("<center>");
[Link]("<h1>Hello Servlet!</h1>");
[Link]("</center>");
[Link]("</body>");
[Link]("</html>");
}
}

[Link]

<html>
<head>
<title>Simple Servlet</title>
</head>
< body bgcolor="dodgerblue">
<center>
<h2><a href=./myservlet>Simple Servlet</a></h2>
</center>
</body>
</html>

Execution Procedure for Servlets:

1. Create your own folder. Eg : servletdemos

2. Create a folder with name “WEB-INF“in the root directory(servletdemos)

3. Create a folder with name “classes “in WEB-INF folder.

4. Compile the java file and place “ .class “ file in “ classes “ folder.

5. Place “ [Link] “ file in root directory.

6. Create a “ war “ file in root directory as follows:

C:\> < path>\ <root directory name > jar cvf [Link] *.* [Link]

7. Run the Tomcat Service.

8. Open Tomcat Manager by typing “ [Link] “in Browser.


9. Click on “Tomcat Manager”.

10. Deploy the created “ war file “.

11. Click on war file to execute the Servlet.

Output:-
2 . Servlet Program to accept form data and print

[Link]

<?xml version="1.0"?>
<web-app>
<servlet>
<servlet-name>printformdata</servlet-name>
<servlet-class>Printformdata</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>printformdata</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
</web-app>

[Link]
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class Printformdata extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
[Link]("text/html");
PrintWriter pw = [Link]();

String username = [Link]("username");


String password = [Link]("password");

[Link]("<html>");
[Link]("<head>");
[Link]("<title>Form Servlet!</title>");
[Link]("</head>");
[Link]("<body bgcolor=orange>");
[Link](username);
[Link]("<br>");
[Link](password);
}
}

[Link]
<html>
<head>
<title>Login Form</title>
</head>
<body bgcolor="green">
<form method="post" action="./myservlet">
Enter Name:&nbsp; <input type="text" name="username" size="20">
Enter Password: <input type="password" name="password" size="20">
<input type="submit" value="Submit" name="B1">
</form>
</body>
</html>

Output:
[Link] Program to implement ServletConfig and ServletContext.
[Link]

<?xml version="1.0" encoding="ISO-8859-1"?>


<web-app>
<servlet>
<servlet-name> ExServletConfig </servlet-name>
<servlet-class>ExServletConfig</servlet-class>
<init-param>
<param-name>Name</param-name>
<param-value>Veena</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name> ExServletConfig </servlet-name>
<url-pattern>/myservlet</url-pattern>
<context-param>
<param-name>Phone</param-name>
<param-value>Vivo</param-value>
</context-param>
</servlet-mapping>
</web-app>

[Link]

import [Link].*;
import [Link].*;
import [Link].*;
public class ExServletConfig extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
[Link]("text/html");
PrintWriter pw = [Link]();

ServletConfig config=getServletConfig();
ServletContext sc=getServletContext();

String str1=[Link]("Name");
String str2=[Link](("Phone");
[Link](str1);
[Link](str2);
[Link]();
}
}

Output:- Veena
Vivo
4. Servlet Program to accept form data and insert into database

[Link]

<?xml version="1.0" encoding="ISO-8859-1"?>


<web-app>
<servlet>
<servlet-name>ServletInsert</servlet-name>
<servlet-class>ServletInsert</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletInsert</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
</web-app>

[Link]
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class ServletInsert extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
[Link]("text/html");
PrintWriter pw = [Link]();
Connection con;
try
{
String username = [Link]("username");
String password = [Link]("password");
[Link](username);
[Link](password);
[Link]("[Link] ");
con=[Link](jdbc:oracle:thin:@localhost:1521:xe","system"
,"manager");
PreparedStatement pst = [Link]("insert into login
values(?,?)");
[Link](1,username);
[Link](2,password);
int i = [Link]();
if(i!=0)
{
[Link]("<br>Record has been inserted");
}
else
{
[Link]("failed to insert the data");
}
}
catch (SQLException e)
{
[Link](e);
}
catch(ClassNotFoundException e)
{
[Link](e);
}
}
}

[Link]

<html>
<head>
<title>Login Form</title>
</head>
<body bgcolor="green">
<form method="POST" action="./myservlet">
Enter Name:&nbsp;<input type="text" name="username" size="20">
Enter Password: <input type="password" name="password" size="20">
<input type="submit" value="Submit" name="B1">
</form>
</body>
</html>

Output:-

5. Servlet Program to retrieve data from database.

[Link]
<?xml version="1.0"?>
<web-app>
<servlet>
<servlet-name>RetrieveData</servlet-name>
<servlet-class>RetrieveData</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RetrieveData</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
</web-app>
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class RetrieveData extends HttpServlet
{
Connection con;
Statement stmt;
ResultSet rs;
public void doGet(HttpServletRequest req, HttpServletResponse res) throws
ServletException,IOException
{
[Link]("text/html");
PrintWriter pw=[Link]();
try
{
[Link]("[Link]");

con=[Link]("jdbc:oracle:thin:@localhost:1521:xe","system","manage
r");
stmt=[Link]();
rs=[Link]("select * from login");
while([Link]())
{
String username=[Link]("username");
String password=[Link]("password");
[Link](username);
[Link](password);
}
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}
[Link]
<html>
<head>
<title>Simple Servlet</title>
</head>
<body bgcolor="yellow">
<center>
<h2><a href=./myservlet>Retrieve Data</a></h2>
</center>
</body>
</html>

Output:-
6. Servlet Program to manage sessions

[Link]

<?xml version="1.0" encoding="ISO-8859-1"?>


<web-app>
<servlet>
<servlet-name>Exservlet</servlet-name>
<servlet-class>Exservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Exservlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SessionSample</servlet-name>
<servlet-class>SessionSample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionSample</servlet-name>
<url-pattern>/sessiondemo</url-pattern>
</servlet-mapping>
</web-app>

[Link]

import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class SessionSample extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Get the Session object
boolean create = true;
HttpSession session = [Link](create);
// Get the session data value
Integer ival = (Integer)[Link] ("[Link]");
if (ival == null) ival = new Integer (1);
else
ival = new Integer ([Link] () + 1);
[Link] ("[Link]", ival);
// Output the page
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html>");
[Link]("<head><title>Session Tracking Test</title></head>");
[Link]("<body>");
[Link]("<h1>Session Tracking Test</h1>");
[Link] ("You have hit this page " + ival + " times" + "<br>");
[Link] ("Your " + [Link]("Cookie"));
[Link]("</body></html>");
}
}

[Link]

<html>
<body>
<a href="./login">dbservlet</a>
<a href="./sessiondemo">Session Servlet</a>
</body>
</html>
Output:-

You might also like