0% found this document useful (0 votes)
35 views7 pages

IP Lab 1session (2018506125)

Integrated programming session
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)
35 views7 pages

IP Lab 1session (2018506125)

Integrated programming session
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
You are on page 1/ 7

SERVLET SESSION

DATE : 03/03/21
EX No :1a)

1a) Hit Counter for web page using a servlet

AIM:
To write a simple servlet code to display a message and calculate hit count.

PROCEDURE:

1)Open Eclipse and then click File ❯ New ❯ Click Dynamic Web Project.
2)Name the Project.
3) Create a servlet by Right clicking on the src folder and create a new class file
and name the file.
4)Add jar file in eclipse IDE:right click on your project -> Build Path ->
Configure Build Path ->click on Libraries tab in Java Build Path -> click on Add External
JARs button -> select the servlet-api.jar file under tomcat/lib -> ok.
5)Create an html page to call the servlet class on a webpage in WebContent folder(
WebContent/index.html)
6)Edit web.xml file.we will map the Servlet with the specific URL.
7) Start the server and deploy the project: Right click on your project -> Run As ->
Run on Server -> choose tomcat server -> next -> addAll -> finish.

PROGRAM:

Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>PageHitCounter</servlet-name>
<servlet-class>PageHitCounter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PageHitCounter</servlet-name>
<url-pattern>/PageHitCounter</url-pattern>
</servlet-mapping>
</web-app>

PageHitCounter.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/PageHitCounter")
public class PageHitCounter extends HttpServlet {
private static final long serialVersionUID = 1L;
private int hitCount;
public void init() {
hitCount = 0;
}
public PageHitCounter() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)throws
ServletException, IOException {
response.setContentType("text/html");
hitCount++;
PrintWriter out = response.getWriter();
String title = "Total Number of Hits";
String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";

out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor = \"#800080\">\n" +
"<h1 align = \"center\">" + title + "</h1>\n" +
"<h2 align = \"center\">" + hitCount + "</h2>\n" +
"</body> </html>"
);
}
}

RESULT:

HitCount has been calculated using session in servlet.

OUTPUT:
1b)Create Login form and display welcome message using session in servlet.

AIM:
To write a simple servlet code to create a login form and display a welcome message
using session in servlet.

PROCEDURE:

1)Open Eclipse and then click File ❯ New ❯ Click Dynamic Web Project.
2)Name the Project.
3) Create a servlet by Right clicking on the src folder and create a new class file
and name the file.
4)Add jar file in eclipse IDE:right click on your project -> Build Path ->
Configure Build Path ->click on Libraries tab in Java Build Path -> click on Add External
JARs button -> select the servlet-api.jar file under tomcat/lib -> ok.
5)Create an html page to call the servlet class on a webpage in WebContent folder(
WebContent/index.html)
6)Edit web.xml file.we will map the Servlet with the specific URL.
7) Start the server and deploy the project: Right click on your project -> Run As ->
Run on Server -> choose tomcat server -> next -> addAll -> finish.

PROGRAM:

Web.xml

<?xml version="1.0" encoding="UTF-8"?>


<web-app>
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-class>MyServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
MyServlet1.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.*;
@WebServlet("/MyServlet1")
public class MyServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
public MyServlet1() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
response.setContentType("text/html");
PrintWriter pwriter = response.getWriter();

String name = request.getParameter("userName");


String password = request.getParameter("userPassword");
pwriter.println("Hello "+name+" !!");
pwriter.println("Your Password is: "+password);
HttpSession session=request.getSession();
session.setAttribute("uname",name);
session.setAttribute("upass",password);
pwriter.close();
}catch(Exception exp){
System.out.println(exp);
}
}
}

index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>LOGIN PAGE</title>
</head>
<body>
<form action="login">
User Name:<input type="text" name="userName"/><br/>
Password:<input type="password" name="userPassword"/><br/>
<input type="submit" value="SUBMIT"/>
</form>
</body>
</html>

RESULT:

Login form is created and welcome message is displayed using session in servlet.

OUTPUT:

You might also like