15. Assume that we have got three pdf files for the MCA-1 Syllabus, MCA-2 Syllabus and MCA-3 Syllabus respectively, Now write a Servlet which displays the appropriate PDF file to the client, by looking at a request parameter for the year (1, 2 or 3).
Index.html
<html>
<head>
<title>EX-15: WTAD : www.bipinrupadiya.com</title>
</head>
<body>
<center>
<FORM action="/Ex15/bipinrupadiya.blogspot.com">
Select MCA Syllabus : <select size=1 name="MCA">
<option value="MCA-1">MCA1</option>
<option value="MCA-2">MCA2</option>
<option value="MCA-3">MCA3</option>
</select>
<INPUT TYPE="SUBMIT" value="Show" >
</FORM>
</center>
</body>
</html>
Servlet<head>
<title>EX-15: WTAD : www.bipinrupadiya.com</title>
</head>
<body>
<center>
<FORM action="/Ex15/bipinrupadiya.blogspot.com">
Select MCA Syllabus : <select size=1 name="MCA">
<option value="MCA-1">MCA1</option>
<option value="MCA-2">MCA2</option>
<option value="MCA-3">MCA3</option>
</select>
<INPUT TYPE="SUBMIT" value="Show" >
</FORM>
</center>
</body>
</html>
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet(
name="WTAD",
urlPatterns={"/Ex15","/bipinrupadiya.blogspot.com"}
)
public class ShowPDF extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws
ServletException,IOException
{
String sem = request.getParameter("MCA");
response.setContentType("application/pdf");
response.sendRedirect("/"+sem+".pdf");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException
{
doGet(request,response);
}
}
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet(
name="WTAD",
urlPatterns={"/Ex15","/bipinrupadiya.blogspot.com"}
)
public class ShowPDF extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws
ServletException,IOException
{
String sem = request.getParameter("MCA");
response.setContentType("application/pdf");
response.sendRedirect("/"+sem+".pdf");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException
{
doGet(request,response);
}
}
NOTE: Put Three .pdf file on application's root directory (MCA-1.pdf, MCA-2.pdf, MCA-3.pdf)
14. Write a Servlet which displays a message and also displays how many times the message has been displayed (how many times the page has been visited).
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import javax.servlet.annotation.WebServlet;
@WebServlet("/PageVisitCounter")
public class PageVisitCounter extends HttpServlet
{
public void doGet(HttpServletRequest req , HttpServletResponse res)
throws IOException , ServletException
{
PrintWriter out = res.getWriter();
Cookie[] myCookie = req.getCookies();
boolean found = false;
int v=0;
if(myCookie != null)
{
for(int i=0; i<myCookie.length;i++)
{
if(myCookie[i].getName().equals("pageCount"))
{
v = Integer.parseInt(myCookie[i].getValue());
v++;
Cookie c1 = new Cookie("pageCount",String.valueOf(v));
res.addCookie(c1);
out.println("Visit No.:"+v);
found = true;
break;
}
}
}
if(found==false)
{
Cookie c1 = new Cookie("pageCount",String.valueOf(v));
res.addCookie(c1);
out.println("<h1>Welcome to www.BipinRupadiya.com site</h1><br><br><br>You have done first visit..!");
}
}
}
NOTE : Enable Cookie in your browser to run this example.
13. Write a Servlet to display all the attributes available from request and context.
import javax.servlet.*;
import java.util.*;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet(
name="WTAD",
urlPatterns={"/Ex13","/bipinrupadiya.blogspot.com"}
)
public class RequestAttributes2 extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<br> Server Port : " + request.getServerPort());
out.println("<br> Server Name : " + request.getServerName());
out.println("<br> Protocol : " + request.getProtocol());
out.println("<br> Character Encoding : " + request.getCharacterEncoding());
out.println("<br> Content Type : " + request.getContentType());
out.println("<br> Content Length : " + request.getContentLength());
out.println("<br> Remote Address : " + request.getRemoteAddr());
out.println("<br> Remote Host : " + request.getRemoteHost());
out.println("<table border=1 align='CENTER'><tr bgcolor='#FFAD00'>") ;
out.println("<th>Header Name</th><th>Header Value</th></tr>");
Enumeration parameters = request.getParameterNames();
while(parameters.hasMoreElements())
{
String ParaName= (String) parameters.nextElement();
out.println("<tr><td>" + ParaName+"</td>");
out.println("<td>" + request.getParameter(ParaName)+"</td></tr>");
}
out.println("</table><br><br>");
out.println("<table border=1 align='CENTER'><tr bgcolor='#FFAD00'>") ;
out.println("<th>Attribute Name</th><th>Attribute Value</th></tr>");
Enumeration attributes = request.getAttributeNames();
while(attributes.hasMoreElements())
{
String attName = (String) attributes.nextElement();
out.println("<tr><td>" + attName+"</td>");
out.println("<td> " + request.getAttribute(attName)+"</td></tr>");
}
out.println("</table>");
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}
12. Write a Servlet to display parameters available on request.
Index.html
<html>
<head>
<title>EX-12: WTAD : www.bipinrupadiya.com</title>
</head>
<body>
<form action="/Ex12/bipinrupadiya.blogspot.com" method="get">
<br>User:<input type="text" name="usr">
<br>
Password : <input type="text" name="pwd">
<br>
<input type="Submit" value="Login"><input type="reset" value="Clear">
</form>
<body>
</html>
<head>
<title>EX-12: WTAD : www.bipinrupadiya.com</title>
</head>
<body>
<form action="/Ex12/bipinrupadiya.blogspot.com" method="get">
<br>User:<input type="text" name="usr">
<br>
Password : <input type="text" name="pwd">
<br>
<input type="Submit" value="Login"><input type="reset" value="Clear">
</form>
<body>
</html>
Servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet(
name="WTAD",
urlPatterns={"/Ex12","/bipinrupadiya.blogspot.com"}
)
public class readLogin extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<br><B>USER :</B>: "
+ request.getParameter("usr") +
"<br><B>Password</B>: "
+ request.getParameter("pwd") +
"");
}
}
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet(
name="WTAD",
urlPatterns={"/Ex12","/bipinrupadiya.blogspot.com"}
)
public class readLogin extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<br><B>USER :</B>: "
+ request.getParameter("usr") +
"<br><B>Password</B>: "
+ request.getParameter("pwd") +
"");
}
}
output
11. Write a Servlet to display all the headers available from request.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import javax.servlet.annotation.WebServlet;
/** Showing all request headers of current request. */
@WebServlet(
name="WTAD",
urlPatterns={"/Ex11","/bipinrupadiya.blogspot.com","/RequestHeaders"}
)
public class ShowRequestHeaders extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "GTU : Ex-11 : Showing all Request Headers";
out.println("<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +
"<B>Request Method: </B>" +
request.getMethod() + "<BR>\n" +
"<B>Request URI: </B>" +
request.getRequestURI() + "<BR>\n" +
"<B>Request Protocol: </B>" +
request.getProtocol() + "<BR><BR>\n" +
"<TABLE BORDER=1 ALIGN=\"CENTER\">\n" +
"<TR BGCOLOR=\"#FFAD00\">\n" +
"<TH>Header Name<TH>Header Value");
Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String headerName = (String)headerNames.nextElement();
out.println("<TR><TD>" + headerName);
out.println(" <TD>" + request.getHeader(headerName));
}
out.println("</TABLE>\n</BODY></HTML>");
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import javax.servlet.annotation.WebServlet;
/** Showing all request headers of current request. */
@WebServlet(
name="WTAD",
urlPatterns={"/Ex11","/bipinrupadiya.blogspot.com","/RequestHeaders"}
)
public class ShowRequestHeaders extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "GTU : Ex-11 : Showing all Request Headers";
out.println("<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +
"<B>Request Method: </B>" +
request.getMethod() + "<BR>\n" +
"<B>Request URI: </B>" +
request.getRequestURI() + "<BR>\n" +
"<B>Request Protocol: </B>" +
request.getProtocol() + "<BR><BR>\n" +
"<TABLE BORDER=1 ALIGN=\"CENTER\">\n" +
"<TR BGCOLOR=\"#FFAD00\">\n" +
"<TH>Header Name<TH>Header Value");
Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String headerName = (String)headerNames.nextElement();
out.println("<TR><TD>" + headerName);
out.println(" <TD>" + request.getHeader(headerName));
}
out.println("</TABLE>\n</BODY></HTML>");
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Subscribe to:
Posts (Atom)
Subjects
- WordPress
- Mobile Computing-4649303 Practical Solution
- Android Programming New Syllabus Theory
- PHP LAMP Question Bank
- PHP LAMP Theory
- Step by Step Android Example
- Android Practical
- Android Theory
- Android Question Bank
- Networking FON Practical
- Networking FON Theory
- OS Practical
- OS Theory
- HTML
- JavaScript
- J2EE WTAD Theory
- J2EE WTAD Question Bank
- J2EE WTAD Quick Guide
- J2EE WTAD GTU Papers
- J2EE WTAD Practical
- Python
- JAVA Theory
- JAVA Practical
- MIS
Categories
- Android (55)
- CSS (3)
- Configure Tomcat7 (2)
- Decryption (16)
- Difference (1)
- Encryption (16)
- Error Detection and Correction Techniques (3)
- FON (27)
- Framing Technic (2)
- J2EE (29)
- JAVA (13)
- JavaScript (19)
- OS (17)
- PHP (11)
- Protocol (3)
- SERVER SOCKET PROGRAMING (7)
- Servlet (13)
- WTAD (34)
- c (11)
- install Tomcat (2)
- linux (8)
- shell script (33)
- unix (22)
Total Pageviews
© BipinRupadiya.com. Powered by Blogger.
