Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views

Assignment3 Java

Uploaded by

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

Assignment3 Java

Uploaded by

Ansari Faisal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Name :Shaikh kashish Zaheer

Roll No: 22

Assignment -3 Servlet
SET-A
Q1:Write a Java Servlet to display Hello World.
=>
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Hello extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse
response)
throws IOException ,ServletException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("Hello World");
out.println("</body>");
out.println("</html>");
}
}
Output:
Hello World

Q2:Write a Java Servlet to accept two numbers from user and perform
their addition.
=>
//add.html
<html>
<body>
<form action="/tybcs15/Add">
<pre>
Enter first number:
<input type="text" name="n1">
Enter Second number:
<input type="text" name="n2">
<input type="submit" name="submit" value="Add">
</pre>
</form>
</body>
</html>
//Add.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Add extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse
response)
throws IOException ,ServletException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String a=request.getParameter("n1");
String b=request.getParameter("n2");
int a1=Integer.parseInt(a);
int b1=Integer.parseInt(b);
int sum=(a1+b1);
out.println("Sum is:"+sum);
}
}
Q3:Write a Java Servlet to accept a user name and greet the user.
=>
//greet.html
<html>
<body>
<form action="/tybcs15/Greet">
<pre>
Enter Your name:
<input type="text" name="n1">
<input type="submit" name="submit">
</pre>
</form>
</body>
</html>
//Greet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Greet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse
response)
throws IOException ,ServletException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String a=request.getParameter("n1");
out.print("hello And Welcome"+a);
}
}
Q4:Design a servlet that provides information about a HTTP request
from a client, such as IP address and browser type. The Servlet also
provides information about server on which the servlet is running, such
as the operating system type, and the names of currently loaded servlets.
=>
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Info extends HttpServlet
{
public void doGet(HttpServletRequest rq,HttpServletResponse rs)
throws IOException ,ServletException
{
rs.setContentType("text/html");
PrintWriter out=rs.getWriter();
out.print("<html>");
out.print("<body>");
out.print("Server Name:"+rq.getServerName()+"<br>");
out.print("Remote Address:"+rq.getRemoteAddr()+"<br>");
out.print("Remote User:"+rq.getRemoteUser()+"<br>");
out.print("Server Port:"+rq.getServerPort()+"<br>");
out.print("Remote Host:"+rq.getRemoteHost()+"<br>");
out.print("Local Name:"+rq.getLocalName()+"<br>");
out.print("Servlet Name:"+this.getServletName()+"<br>");
out.print("</body>");
out.print("</html>");
out.close();
}
}
Q5:Write a servlet which counts how many times a user has visited a
web page. If the user is visiting the page for the first time, display a
welcome message. If the user is revisitng the page, display the number of
times visited(Use Cookies).
=>
import javax.servlet.*;
import javax.servlet.http.*;
public class Pagevisit extends HttpServlet
{
public void doGet(HttpServletRequest rq,HttpServletResponse rs)
throws IOException,ServletException
{
rs.setContentType("text/html");
PrintWriter out=rs.getWriter();
Cookie ck[]=rq.getCookies();
out.print("<html>");
out.print("<body>");
if(ck.length!=0)
{
for(int i=0;i<ck.length;i++)
{
if(ck[i].getName().equals("visit"))
{
int cnt=Integer.parseInt(ck[i].getValue());
cnt++;
out.print("Visit Count:" +cnt);
ck[i].setValue(Integer.toString(cnt));
out.close();
return;
}
}
}
out.print("Wel*come.......Visit count=1");
Cookie c=new Cookie("visit","1");
}
}
Q6:Write a java servlet which accepta a form name and background
color from user and display the text in specified font with background
color.
=>
//Bg.html
<html>
<body>
<form action="/tybcs15/Bg">
<pre>
Enter Font Name:
<input type="text" name="n1">
Enter Background Color:
<input type="text" name="n2">
<input type="submit" name="submit" value="Show">
</pre>
</form>
</body>
</html>
//Bg.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Bg extends HttpServlet {


public void doGet(HttpServletRequest rq, HttpServletResponse rs)
throws IOException, ServletException
{
PrintWriter p=rs.getWriter();
String fn=rq.getParameter("n1");
String bg=rq.getParameter("n2");
p.print("<html>");
p.print("<body bgcolor="+bg+">");
p.print("<font face="+fn+"> Welcome Here</font>");
}
}
SET B

Q1:Write a program to create a shopping mall. User must be allowed to


do purchase from two pages each page should have a page total. The
thirs page should display a bill which consist of a page total of whatever
the purchase has been done and print the total(use Http session).
=>
//page1.html
<html>
<head>
<title>Page 1</title>
</head>
<form method="GET" action="/tybcs15/Page1servlet">
<input type="checkbox" name="item" value=100> Book 100<br>
<input type="checkbox" name="item" value=200> CD 200<br>
<input type="checkbox" name="item" value=150> Tape 150<br>
<input type="submit">
<input type="reset">
</form>
</body>
</html>
//Page2.html
<html>
<head>
<title>Page 2</title>
</head>
<form method="GET" action="/tybcs15/Page2servlet">
<input type="checkbox" name="item" value=10000> Printer 10000<br>
<input type="checkbox" name="item" value=200> Pendrive 200<br>
<input type="checkbox" name="item" value=15000>Camera 15000<br>
<input type="submit">
<input type="reset">
</form>
</body>
</html>
//Page1servlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Page1servlet extends HttpServlet {


public void doGet(HttpServletRequest rq, HttpServletResponse rs)
throws IOException, ServletException
{
int sum=0;
String values[]=rq.getParameterValues("item");
if(values!=null)
{
for(int i=0;i<values.length;i++)
sum=sum+Integer.parseInt(values[i]);
}
HttpSession hs=rq.getSession(true);
hs.setAttribute("page1",sum);
rs.sendRedirect("page2.html");
}
}
//Page2servlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Page2servlet extends HttpServlet
{
public void doGet(HttpServletRequest rq,HttpServletResponse rs) throws
IOException,ServletException
{
rs.setContentType("text/html");
PrintWriter p=rs.getWriter();
int sum2=0;
String values[]=rq.getParameterValues("item");
if(values!=null)
{
for(int i=0;i<values.length;i++)
sum2=sum2+Integer.parseInt(values[i]);
}
HttpSession hs=rq.getSession();
int sum1=((Integer)hs.getAttribute("page1")).intValue();
p.print("<html>");
p.print("<title>Summary</title><body>");
p.print("Total of page 1:"+sum1+"<br>");
p.print("Total of page 2:"+sum2+"<br>");
int total=sum1+sum2;
p.print("Total Bill="+total+"<br>");
p.print("</body></html>");
}
}
Q2:Design an html page containing for option buttons(painting,
drawing, singing, swimming). And two buttons reset and sumit. When
the user click submit, the server response by adding a cookie containing
the selected hoobie and sends a message back to client. Proghram should
not allow duplicate cookies to be written.
=>
//Hobby.html
<html>
<body>
<form action="/tybcs15/Hobby">
<pre>
<input type="checkbox" name="h" value="drawing">Drawing<br>
<input type="checkbox" name="h" value="singing">Singing<br>
<input type="checkbox" name="h" value="reading">Reading<br>
<input type="checkbox" name="h" value="writing">Writing<br>
<input type="submit" name="submit" value="Show">
</pre>
</form>
</body>
</html>
//hobby.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Hobby extends HttpServlet
{
public void doGet(HttpServletRequest rq,HttpServletResponse rs) throws
IOException,ServletException
{
rs.setContentType("text/html");
PrintWriter p=rs.getWriter();
String v[]=rq.getParameterValues("h");
p.print("<html>");
p.print("<body>");
p.print("The selected Hobbies are:");
for(int i=0;i<v.length;i++)
{
p.print(v[i]);
}
}
}

You might also like