Servlets: 1. Write An Example Program For Generic Servlet Welcome - Java

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Servlets

1. Write an example program for Generic Servlet

Welcome.java

import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
public class welcome extends GenericServlet
{
public void service(ServletRequest req,ServletResponse res) throws
ServletException,IOException
{
PrintWriter out=res.getWriter();
out.println("<h1>Welcome to Generic Servlets</h1>");
out.close();
}
}

web.xml
<web-app>
<servlet>
<servlet-name>servlets</servlet-name>
<servlet-class>welcome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlets</servlet-name>
<url-pattern>/list</url-pattern>
</servlet-mapping>
</web-app>
2.Write an example for Http Servlet

hello.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class hello extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<h1>hai!</h1>");
out.println("<h2> Hello World ! </h2>");
}
}

web.xml

<web-app>
<servlet>
<servlet-name>servlets1</servlet-name>
<servlet-class>hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlets1</servlet-name>
<url-pattern>/l1</url-pattern>
</servlet-mapping>
</servlet>
</web-app>
3. Write a servlet program for login validation

Login.html

<html>
<head>
<title>New Page 1</title>
</head>
<body>
<h2>Login</h2>
<p>Please enter your username and password</p>
<form method="GET" action="http://localhost:2014/abc/LoginServlet">
<p> Username <input type="text" name="username" size="20"></p>
<p> Password <input type="text" name="password" size="20"></p>
<p><input type="submit" value="Submit" name="B1"></p>
</form>
<p>&nbsp;</p>
</body>
</html>

LoginServlet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginServlet extends HttpServlet


{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws
ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("username");
String pass = request.getParameter("password");
out.println("<html>");
out.println("<body>");
out.println("Thanks Mr." + " " + name + " "
+ "for visiting SAISPURTHI<br>" );
out.println("Now you can see your password : "
+ " " + pass + "<br>");
out.println("</body></html>");
}}
web.xml
<web-app>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>
4.write a servlet program for creation of cookies

cook.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletDemo extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response)


throws IOException{

response.setContentType("text/html");
PrintWriter pw = response.getWriter();

Cookie cookie = new Cookie("url","saisputhi");


cookie.setMaxAge(60*60); //1 hour
response.addCookie(cookie);

pw.println("Cookies created");
}
}

web.xml

<web-app>

<servlet>
<servlet-name>ServletName</servlet-name>
<servlet-class>ServletDemo</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ServletName</servlet-name>
<url-pattern>/Demo</url-pattern>
</servlet-mapping>

</web-app>
5.write a servlet program for cookies

hello.html

<html>
<body>
<form action="http://localhost:2014/c2/cook" method="GET">
First Name: <input type="text" name="first_name">
<br />
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>
</body>
</html>

HelloForm.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class


public class HelloForm extends HttpServlet {

public void doGet(HttpServletRequest request,


HttpServletResponse response)
throws ServletException, IOException
{
// Create cookies for first and last names.
Cookie firstName = new Cookie("first_name",
request.getParameter("first_name"));
Cookie lastName = new Cookie("last_name",
request.getParameter("last_name"));

// Set expiry date after 24 Hrs for both the cookies.


firstName.setMaxAge(60*60*24);
lastName.setMaxAge(60*60*24);

// Add both the cookies in the response header.


response.addCookie( firstName );
response.addCookie( lastName );

// Set response content type


response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Setting Cookies Example";
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=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<ul>\n" +
" <li><b>First Name</b>: "
+ request.getParameter("first_name") + "\n" +
" <li><b>Last Name</b>: "
+ request.getParameter("last_name") + "\n" +
"</ul>\n" +
"</body></html>");
}
}

web.xml

<web-app>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloForm</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/cook</url-pattern>
</servlet-mapping>
</web-app>
6.write a servlet program for cookies

cook1.html

<html>
<head>
<title> cookie</title>
</head>
<body>
<form name="f1" action="http://localhost:2014/c3/c1">
enter name:<input type="text" name="t1">
<input type="submit" value="submit">
</form>
</body>
</html>

cook2.html

<html>
<head>
<title> cookie</title>
</head>
<body>
<form name="f1" action="http://localhost/c3/c2">
enter salary:<input type="text" name="t1">
<input type="submit" value="submit">
</form>
</body>
</html>

cook1.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class cook1 extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws
ServletException,IOException
{
String name=req.getParameter("t1");
Cookie c=new Cookie("name",name);
res.addCookie(c);
res.sendRedirect("cook2.html");
}}
cook2.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class cook2 extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws
ServletException, IOException
{
PrintWriter out=res.getWriter();
String income=req.getParameter("t1");
Cookie c[]=req.getCookies();
for(int i=0;i<c.length;i++)
{
out.println(c[i].getName()+"is"+c[i].getValue());
}
out.println("incomeis"+income);
}
}

web.xml
<web-app>
<servlet>
<servlet-name>cook1</servlet-name>
<servlet-class>cook1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cook1</servlet-name>
<url-pattern>/c1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>cook2</servlet-name>
<servlet-class>cook2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cook2</servlet-name>
<url-pattern>/c2</url-pattern>
</servlet-mapping>
</web-app>
7.write a servlet program for creation of sessions

SessionTrack.java

// Import required java libraries


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

// Extend HttpServlet class


public class SessionTrack extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse response) throws
ServletException, IOException
{
// Create a session object if it is already not created.
HttpSession session = request.getSession(true);
// Get session creation time.
Date createTime = new Date(session.getCreationTime());
// Get last access time of this web page.
Date lastAccessTime = new Date(session.getLastAccessedTime());
String title = "Welcome Back to my website";
Integer visitCount = new Integer(0);
String visitCountKey = new String("visitCount");
String userIDKey = new String("userID");
String userID = new String("ABCD");
// Check if this is new comer on your web page.
if (session.isNew())
{
title = "Welcome to my website";
session.setAttribute(userIDKey, userID);
}
else
{
visitCount = (Integer)session.getAttribute(visitCountKey);
visitCount = visitCount + 1;
userID = (String)session.getAttribute(userIDKey);
}
session.setAttribute(visitCountKey, visitCount);

// Set response content type


response.setContentType("text/html");
PrintWriter out = response.getWriter();
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=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<h2 align=\"center\">Session Infomation</h2>\n" +
"<table border=\"1\" align=\"center\">\n" +
"<tr bgcolor=\"#949494\">\n" +
" <th>Session info</th><th>value</th></tr>\n" +
"<tr>\n" +
" <td>id</td>\n" +
" <td>" + session.getId() + "</td></tr>\n" +
"<tr>\n" +
" <td>Creation Time</td>\n" +
" <td>" + createTime +
" </td></tr>\n" +
"<tr>\n" +
" <td>Time of Last Access</td>\n" +
" <td>" + lastAccessTime +
" </td></tr>\n" +
"<tr>\n" +
" <td>User ID</td>\n" +
" <td>" + userID +
" </td></tr>\n" +
"<tr>\n" +
" <td>Number of visits</td>\n" +
" <td>" + visitCount + "</td></tr>\n" +
"</table>\n" +
"</body></html>");
}
}
web.xml
<web-app>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>SessionTrack</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/session</url-pattern>
</servlet-mapping>
</web-app>

You might also like