Servlets: 1. Write An Example Program For Generic Servlet Welcome - Java
Servlets: 1. Write An Example Program For Generic Servlet Welcome - Java
Servlets: 1. Write An Example Program For Generic Servlet Welcome - Java
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> </p>
</body>
</html>
LoginServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
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;
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
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.*;
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