11. Write a Servlet to display all the headers available from request.
Shared By : Pandya Chaitanya (JVIMS-MCA - Jamnagar ) Show/Hide Program
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import javax.servlet.annotation.*;
/** Shows all the request headers sent on the current request. */
@WebServlet("/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 = "Servlet Example: Showing 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>");
}
/** Since this servlet is for debugging, have it
* handle GET and POST identically.
*/
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
12. Write a Servlet to display parameters available on request.
-----------HTML CODE--------------
<HTML><HEAD><TITLE>Collecting Parameters</TITLE></HEAD>
<BODY BGCOLOR="#FDF5E6">
<H1 ALIGN="CENTER">Collecting Parameters</H1>
<FORM ACTION="http://localhost:8081/CNP/params">
First Parameter: <INPUT TYPE="TEXT" NAME="param1"><BR>
Second Parameter: <INPUT TYPE="TEXT" NAME="param2"><BR>
Third Parameter: <INPUT TYPE="TEXT" NAME="param3"><BR>
<CENTER><INPUT TYPE="SUBMIT"></CENTER>
</FORM>
</BODY>
</HTML>
-----------SERVLET-------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Simple servlet that reads parameters from the
* form data.
*/
public class Params extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Reading Request Parameters";
out.println("<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +
"<UL>\n" +
" <LI><B>param1</B>: "
+ request.getParameter("param1") + "\n" +
" <LI><B>param2</B>: "
+ request.getParameter("param2") + "\n" +
" <LI><B>param3</B>: "
+ request.getParameter("param3") + "\n" +
"</UL>\n" +
"</BODY></HTML>");
}
}
13. Write a Servlet to display all the attributes available from...
import javax.servlet.*;
import java.util.*;
import java.io.*;
import javax.servlet.http.*;
public class RequestParameters implements Servlet
{
public void init(ServletConfig config) throws ServletException
{
}
public void destroy(){}
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("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());
Enumeration parameters = request.getParameterNames();
while(parameters.hasMoreElements())
{
String ParaName= (String) parameters.nextElement();
out.println("Parameter Name : " + ParaName);
out.println("Parameter Value: " + request.getParameter(ParaName));
out.println("<BR>");
}
Enumeration attributes = request.getAttributeNames();
while(attributes.hasMoreElements())
{
String attName = (String) attributes.nextElement();
out.println("<BR> Attribute Name : " + attName);
out.println("<BR> Attribute Value : " + request.getAttribute(attName));
out.println("<BR>");
}
}
public String getServletInfo(){return null;}
public ServletConfig getServletConfig(){return null;}
}
Shared By :Nikhil Prajapati (NSVKMSMCA - Visnagar) Show/Hide Program
/**mca.nikhilprajapati@gmail.com*/
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
/** Shows all the request headers sent on the current request. */
public class prog13 extends HttpServlet {
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String title = "Servlet Example: Showing Request Headers";
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=\"#FDF5E6\">\n" +
"<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +
"<B>Request Method: </B>" +
req.getMethod() + "<BR>\n" +
"<B>Request URI: </B>" +
req.getRequestURI() + "<BR>\n" +
"<B>Request Protocol: </B>" +
req.getProtocol() + "<BR><BR>\n" +
"<TABLE BORDER=1 ALIGN=\"CENTER\">\n" +
"<TR BGCOLOR=\"#FFAD00\">\n" +
"<TH>Header Name<TH>Header Value");
Enumeration headerNames = req.getHeaderNames();
while(headerNames.hasMoreElements())
{
String headerName = (String)headerNames.nextElement();
out.println("<TR><TD>" + headerName);
out.println(" <TD>" + req.getHeader(headerName));
}
out.println("</TABLE>\n<h2>Mail:mca.nikhilprajapati@gmail.com</h2></BODY></HTML>");
}
/** Since this servlet is for debugging, have it
* handle GET and POST identically.
*/
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
14. Write a Servlet which displays a message and also displays...
//WEB.XML FILE
<web-app>
<servlet>
<servlet-name>prog_14</servlet-name>
<servlet-class>counter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>prog_14</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
//M09005
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class counter extends HttpServlet
{
public void doGet(HttpServletRequest req , HttpServletResponse res) throws IOException , ServletException
{
PrintWriter out = res.getWriter();
Cookie[]ck = req.getCookies();
if(ck != null)
{
for(int i=0; i<ck.length;i++)
{
Cookie c = ck[i];
String s = c.getName();
if(s.equals("diz"))
{
String val = c.getValue();
int v = Integer.parseInt(val);
v++;
val=String.valueOf(v);
Cookie nc = new Cookie("diz",val);
res.addCookie(nc);
out.println("Visit No:"+val);
return;
}
}
Cookie c1 = new Cookie("diz","0");
res.addCookie(c1);
out.println("Cookie is created ..refresh the page..");
}
}
}
15. Assume that we have got three pdf files for the MCA-...
// Author : Pandya Chaitanya N.
// College : JVIMS-MCA, JAMNAGAR
// All the PDF files must keep in the Web Folder
// Default Web folder for tomcat 7.0 is examples
// So keep your PDF files in examples
// if you had create your own web folder then put files in that folder only.
// GTU# 15
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShowRequestPDF extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws
ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String st = "Load PDF Files";
out.println("<title>"+ st +"</title><body><center>");
out.println("<FORM METHOD=POST>");
out.println("<select size=1 name=syll><BR><BR>");
out.println("<option>MCA1</option>");
out.println("<option>MCA2</option>");
out.println("<option>MCA3</option>");
out.println("</select><BR><BR>");
out.println("<INPUT TYPE=SUBMIT value=Show>");
out.println("</FORM>");
out.println("</center></body>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException
{
String st = request.getParameter("syll");
response.setContentType("application/pdf");
if(st.equals("MCA1"))
{
response.sendRedirect("MCA_I.pdf");
}
else if(st.equals("MCA2"))
{
response.sendRedirect("MCA_II.pdf");
}
else if(st.equals("MCA3"))
{
response.sendRedirect("MCA_III.pdf");
}
}
}
Shared By : Chirag Daxini Show/Hide Program
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Ex15 extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException
{
String a;
res.setContentType("application/pdf");
PrintWriter out=res.getWriter();
a=req.getParameter("pdfd");
//res.sendRedirect("http://localhost:8080/dc1.pdf");
if(a.equals("pdf1"))
{
res.sendRedirect("http://localhost:8080/dc1.pdf");
}
if(a.equals("pdf2"))
{
res.sendRedirect("http://localhost:8080/dc2.pdf");
}
if(a.equals("pdf3"))
{
res.sendRedirect("http://localhost:8080/dc3.pdf");
}
}
}
16. Assume that the information regarding the marks for all the...
NOTE : 1. use this DATABASE (download)
2. create system dsn not user dsn...
---------------------------------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class DisplayMarksheet extends HttpServlet
{
private String en;
public void init()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Loads Driver for Connection
}
catch(ClassNotFoundException nf)
{
//System.out.println(nf);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE> STUDENT MARKSHEET....</TITLE>");
out.println("</HEAD>");
out.println("<BODY BGCOLOR=#D8BFD8>");
out.println("<CENTER>");
out.println("<H1> Marksheet...</H1>");
out.println("<FORM METHOD=POST>");
out.println("Enrollment No.: <input type=TEXT name = T1 >");
out.println("<BR><BR><input type=submit value=SEARCH>");
out.println("</FORM>");
out.println("</CENTER>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//doGet(request, response);
en = request.getParameter("T1");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<center>");
String sql = "select enroll,scode,sname,grade";
sql = sql + " from marks";
sql = sql + " where enroll = " + en + ";";
try
{
Connection con = DriverManager.getConnection("jdbc:odbc:stud");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
out.println("<h1> Marksheet of SEM-III </h1>");
out.println("<BR><b> Enrollment No.:</b>" + en);
out.println("<BR><BR>");
out.println("<TABLE BORDER=1 COLOR=RED BGCOLOR=#F8F8FF>");
out.println("<TH ALIGN=CENTER> SUBJECT CODE </TH>");
out.println("<TH ALIGN=CENTER> SUBJECT NAME </TH>");
out.println("<TH WIDTH=15 ALIGN=CENTER> GRADE </TH>");
if(rs == null)
{
out.println("<h2> No Records.... </h2>");
}
else
{
while(rs.next())
{
out.println("<TR>");
out.println("<TD ALIGN=CENTER>");
out.println(rs.getString(2));
out.println("</TD>");
out.println("<TD ALIGN=CENTER>");
out.println(rs.getString(3));
out.println("</TD>");
out.println("<TD ALIGN=CENTER>");
out.println(rs.getString(4));
out.println("</TD>");
out.println("</TR>");
}
rs.close();
st.close();
con.close();
}
}
catch(SQLException sqlex)
{
out.println(sqlex);
}
catch(Exception e)
{
out.println(e);
}
out.println("</TABLE>");
out.println("</CENTER>");
out.println("<BODY>");
out.println("</BODY>");
out.println("</HTML>");
}
}
17. Develop a Servlet which looks for cookies for username and...
Shared By : Your Name (College - Place ) Show/Hide Program
Share this program...
Send it to gtumca@gmail.com
with your Name - College name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
18. Develop a Servlet to authenticate a user, where the...
Shared By : Divyang Panchal (D.L.Patel - Himmatnagar ) Show/Hide Program
<!__ Note : Put this file in myapp folder
enter username=dev
Password=devdip __>
<html>
<body>
Note : enter username=dev
Password=devdip
<form method="post" action="/myapp/servlet/pra18">
UserName : <input type="text" name="un"><br/>
Password : <input type="password" name="pw"><br/>
<input type="submit" value="Submit" >
</form>
</body>
</html>
<!__ put this file in myapp folder __>
<html>
<body>
<h1>Well Come To My Home Page</h1>
<%
HttpSession sessi=request.getSession();
%>
Name : <%= session.getAttribute("Name") %><br/>
SurName : <%= session.getAttribute("Sur") %><br/>
Adsress : <%= session.getAttribute("Add") %><br/>
</body>
</html>
// put this file in classes folder
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class pra18 extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String user=request.getParameter("un");
String pass=request.getParameter("pw");
if(user.equals("dev") && pass.equals("devdip"))
{
out.println("<html>");
out.println("<body>");
out.println("<form>");
out.println("Name : <input type=text name=nam ><br/>");
out.println("Surname : <input type=text name=snam ><br/>");
out.println("Address : <input type=text name=add><br/>");
out.println("<input type=submit value=submit>");
}
else
{
response.sendRedirect("../p18.html");
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException
{
HttpSession session=request.getSession();
session.setAttribute("Name",request.getParameter("nam"));
session.setAttribute("Sur",request.getParameter("snam"));
session.setAttribute("Add",request.getParameter("add"));
response.sendRedirect("../home.jsp");
}
}
19. Write a simple JSP page to display a simple message..
Shared By :Chintan Dave
(Shri Chimanbhai Patel Post Graduate Institute of Computer ) Show/Hide Program
(Shri Chimanbhai Patel Post Graduate Institute of Computer ) Show/Hide Program
<html>
<head>
<title></title>
</head>
<body>
<%= "Hello.........!!!"%>
</body>
</html>
20. Write a JSP page, which uses the include directive to show...
Shared
By :Chintan Dave
(Shri Chimanbhai Patel Post Graduate Institute of Computer ) Show/Hide Program
(Shri Chimanbhai Patel Post Graduate Institute of Computer ) Show/Hide Program
Header
------
<html>
<head>
<title></title>
</head>
<body>
<%="<center>Header::Welcome</center>"%>
</body>
</html>
------
Footer
------
<html>
<head>
<title></title>
</head>
<body>
<%="Footer: Date:"%>
<%= new java.util.Date()%>
</body>
</html>