0% found this document useful (0 votes)
45 views20 pages

1.write A Servlet Program Reading Form Data Using Get and Post Methods

The document contains code for 4 Java servlet programs: 1. A servlet that reads form data using GET and POST methods 2. A servlet that demonstrates HTTP status codes 3. A servlet that handles errors when parsing form input 4. A servlet that sets and reads cookies to store user data between requests Each servlet program contains HTML and Java code to demonstrate the servlet functionality.

Uploaded by

ADARSH MAYYIL
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)
45 views20 pages

1.write A Servlet Program Reading Form Data Using Get and Post Methods

The document contains code for 4 Java servlet programs: 1. A servlet that reads form data using GET and POST methods 2. A servlet that demonstrates HTTP status codes 3. A servlet that handles errors when parsing form input 4. A servlet that sets and reads cookies to store user data between requests Each servlet program contains HTML and Java code to demonstrate the servlet functionality.

Uploaded by

ADARSH MAYYIL
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/ 20

ADARSH M M-19cs801002

1.Write a Servlet program Reading form data using get and post
methods.

Pgm1.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action = "http://localhost:8080/Test/pgm1_servlet" method =
"POST">
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>

Pgm1_servlet.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;

/**
* Servlet implementation class pgm1_servlet
*/
@WebServlet("/pgm1_servlet")
public class pgm1_servlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public pgm1_servlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at:
").append(request.getContextPath());

//GET
String x=request.getParameter("first_name");
String y=request.getParameter("last_name");
PrintWriter writer=response.getWriter();
writer.println(x);
writer.println(y);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//doGet(request, response);

//POST
String x=request.getParameter("first_name");
String y=request.getParameter("last_name");
PrintWriter writer=response.getWriter();
writer.println(x);
writer.println(y);
}
}

Output

2.Write a Servlet program for HTTP status.

Pgm2.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="http://localhost:8080/Test/pgm2">
Enter : <input type="text" name="page">
<br>
<input type="submit" value="Search">
</form>

</body>
</html>

Pgm2.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;

/**
* Servlet implementation class pgm2
*/
@WebServlet("/pgm2")
public class pgm2 extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public pgm2() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String page=request.getParameter("page");
if(page!=null && page.equals("Adarsh"))
{
response.setStatus(HttpServletResponse.SC_OK);
PrintWriter out=response.getWriter();
out.println("<html>"+"<h1>Successful</h1>");
}
else
{

response.sendError(HttpServletResponse.SC_NOT_FOUND,"The
requested page["+page+"]not found.");
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

Output
3.Write a Servlet program for Error Handling.

Pgm3.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="get" action="http://localhost:8080/Test/pgm3">
First Number : <input type="text" name="x"><br><br>
Second Number : <input type="text" name="y"><br><br>
<input type="submit" name="add"><br><br>
<input type="reset" name="clear">
</form>
</body>
</html>
Pgm3.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;

/**
* Servlet implementation class pgm3
*/
@WebServlet("/pgm3")
public class pgm3 extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public pgm3() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at:
").append(request.getContextPath());

try
{
response.setContentType("text/html");
//Integer.parseIntto covert text to number
int a=Integer.parseInt(request.getParameter("x"));
int b=Integer.parseInt(request.getParameter("y"));
int c=a+b;
PrintWriter out= response.getWriter();
out.println("<html><h1>The Sum of "+a+" and "+b+" is
"+c);
}
catch(Exception e)
{
PrintWriter out=response.getWriter();
out.println("<html><h1>Invalid number format</h1>");
}

/**
* @see HttpServlet#doPost(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

Output
4.Write a Java Servlet program to display Cookie.

Pgm4.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form method="post" action="http://localhost:8080/Test/pgm4">


First Name : <input type="text" name="fname"><br><br>
Last Name : <input type="text" name="lname"><br><br>
<input type="submit">
</form>
</body>
</html>

Pgm4.java

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

/**
* Servlet implementation class pgm4
*/
@WebServlet("/pgm4")
public class pgm4 extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public pgm4() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at:
").append(request.getContextPath());

Cookie fName=new
Cookie("first_name",request.getParameter("fname"));
Cookie lName=new
Cookie("last_name",request.getParameter("lname"));
fName.setMaxAge(60*60);
lName.setMaxAge(60*60);
response.addCookie(fName);
response.addCookie(lName);
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String title="Settings Cookies Example";
out.println(request.getParameter("fname"));
out.println(request.getParameter("lname"));
out.print("<form
action='http://localhost:8080/Test/pgm4_b'>");
out.print("<input type='submit' value='View Cookie'>");
out.print("</form>");

out.close();

/**
* @see HttpServlet#doPost(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

Pgm4B.java

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class pgm4_b
*/
@WebServlet("/pgm4_b")
public class pgm4_b extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public pgm4_b() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at:
").append(request.getContextPath());

response.getWriter().append("Served at:
").append(request.getContextPath());
Cookie cookie=null;
Cookie[] cookies=null;
cookies = request.getCookies();
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String title="reading Cookies Example";
if(cookies!=null)
{
out.println("<h2>Found Cookies Name and value</h2>");
for(int i=0;i<cookies.length;i++)
{
cookie=cookies[i];
out.println(cookie.getName()+",");
out.println(cookie.getValue()+"<br>");
}
}
else
{
out.println("</h2>No cookies found</h2>");
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

Output
6.Write a JSP program to perform basic Arithmetic functions.

Pgm6.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="http://localhost:8080/Test/pgm6.jsp" method="post">


<label for="num1"><b>Number1</b></label>
<input type="text" name="num1"><br><br>
<label for="num2"><b>Number2</b></label>
<input type="text" name="num2"><br><br>
<input type="submit" value="Calculate">
</form>

</body>
</html>
Pgm6.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<body>
<% float num1=Integer.parseInt(request.getParameter("num1"));
float num2=Integer.parseInt(request.getParameter("num2"));%>
<%= "addition " +(num1+num2) %><br>
<%= "subtraction " +(num1-num2) %><br>
<%= "multiplication " +(num1*num2) %><br>
<%= "division " +(num1/num2) %><br>
<%= "Square root " +(Math.sqrt(num1)) %><br>
</body>

</body>
</html>

Output
7. Write a JSP program for handling cookies.

Cookie7.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="http://localhost:8080/Test/cookieDemo7.jsp"
method="post">
<p>Enter Your Name</p>
<input type="text" name="username">
<input type="submit" value="submit">
</form>
</body>
</html>

CookieDemo7.jspl

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<% String username=request.getParameter("username");


if(username==null)
{
username="";
}
Cookie cookie=new Cookie("username",username);
cookie.setMaxAge(3);
response.addCookie(cookie);

//Date date=new Date();


//String temp=date.toString();
//System.out.println(temp);
%>
<a href="http://localhost:8080/Test/view7.jsp">Click Here to view the
Cookie</a>
</body>
</html>
view7.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<%
String cookieName="username";
Cookie cookie[]=request.getCookies();
Cookie mycookie=null;
if(cookie!=null)
{
for(int i=0;i<cookie.length;i++)
{
if(cookie[i].getName().equals(cookieName))
{
mycookie=cookie[i];
break;
}
}

%>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
if(mycookie==null)
{%>
No cookie found with the name
<%= cookieName %>
<%}
else
{%>
<p>Welcome : <%=mycookie.getValue() %>
<%} %>

</body>
</html>
Output

You might also like