- Write a Servlet to display all the headers available from request
//----------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class requestheader extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out= res.getWriter();
out.println("<HTML>\n" +" <head> <title> TABLE OF REQUEST HEADERS</title></head>\n"
+ "<body> <h1> TABLE OF REQUEST HEADERS</h1><b>REQUEST METHOD : </b>"+req.getMethod()
+"<b>REQUEST URI :</B>" + req.getRequestURI()
+ " <b> REQUEST PROTOCOL </B> "+ req.getProtocol()
+"<table border=1 align=\*CENTER\*> \n <tr> <th>HEADER NAME </th><th> HEADER VALUE </th>");
Enumeration headers =req.getHeaderNames();
while(headers.hasMoreElements())
{
String headnam = (String)headers.nextElement();
out.prinln("<tr><td>" + headers);
out.println("<TR><TD>" + req.getHeader(headnam));
}
}
} - Write a Servlet to display parameters available on request.
//----------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class paramvalue extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) extends ServetException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String title="Reading All Request parameters";
Enumeration params =req.getParametersNames();
while(params.hasMoreElements())
{
String pnam = (String)params.nextElement();
out.prinln("<tr><td>" + pnam);
out.println("<TR><TD>" + req.getParameter(pnam));
}
}
} - Write a Servlet to display all the attributes available from request and context.
//----------
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShowRequestAndSessionAttributes extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<hrml><body><br><br>");
//Request Attributes....
req.setAttribute("name","Rakesh");
req.setAttribute("course","MCA");
req.setAttribute("collage","LD Engineering");
Enumeration nameOfAttrib=req.getAttributeNames();
out.println("<table border='1'><tr><th colspan=2>List of all request Attributes</th></tr>");
out.println("<tr><td>Attribute Name</td><td>Attribute Value</td></tr>");
String attribName;
while(nameOfAttrib.hasMoreElements())
{
attribName=(String)nameOfAttrib.nextElement();
out.println("<tr><td>"+attribName+"</td><td>");
Object obj=req.getAttribute(attribName);
out.println(""+obj+"</td><tr>");
}
out.println("</table>");
//Context attributes..
ServletContext con=getServletContext();
con.setAttribute("Enrollement Number","095160693024");
con.setAttribute("Seat Number","c-456464");
out.println("<br><br>");
Enumeration contextAttrib=con.getAttributeNames();
out.println("<table border='1'><tr><th colspan=2>List of all Session Attributes</th></tr>");
out.println("<tr><td>Attribute Name</td><td>Attribute Value</td></tr>");
while(contextAttrib.hasMoreElements())
{
attribName=(String)contextAttrib.nextElement();
out.println("<tr><td>"+attribName+"</td><td>");
Object obj=con.getAttribute(attribName);
out.println(""+obj+"</td><tr>");
}
out.println("</table>");
out.println("</body></html>");
}
public void doPost(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException{
doGet(req,res);
}
} - 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.*;
public class accesscounter extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
String count=CookieUtilities.getCookieValue(req,"AccessCount","1");
int count=1;
try
{
count=Integer.parseInt(countString);
}
catch(NumberFormatException nfe)
{}
LongLiveCookie c=new LongLiveCookie("accessCount",String.valueOf(count+1));
res.addCookie(c);
res.setContentType("text/html");
printWriter out=res.getWriter();
String title="Access Count Servlet";
out.println("<html> \n" + "<head> <title> message </title> </head>\n" + "<h2>This is visit number "+ count + "by this browser.</h2>\n" + "</html>");
}
} - 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).
//----------
Html file
------------
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form action="http://localhost:8080/GTU/Ex15" method="get">
<table>
<tr>
<th>Select semester to download syllabus</th>
</tr>
<tr>
<td>
<select name="sem">
<option value="Semester1">Semester1</option>
<option value="Semester2">Semester2</option>
<option value="Semester3">Semester3</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" value="Download"></td>
</tr>
</table>
</form>
</body>
</html>
Servlet File ::
---------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class Ex15 extends HttpServlet {
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<body><br><br><br><table border=1 align='center'>");
String[] paramValues = req.getParameterValues("sem");
String semValue=paramValues[0];
if(semValue.equals("Semester1"))
{
res.sendRedirect("http://gtu.ac.in/syllabus/MCA/MCA%20SEM-1,2%20DETAILED%20SCHEME_SYLLABUS.zip");
}
else if(semValue.equals("Semester2"))
{
res.sendRedirect("http://gtu.ac.in/syllabus/MCA/MCA%20SEM-1,2%20DETAILED%20SCHEME_SYLLABUS.zip");
}
else if(semValue.equals("Semester3"))
{
res.sendRedirect("http://gtu.ac.in/syllabus/MCA/MCA_SEM_-_III_&_SEM_IV_Syllabus.zip");
}
else
{
out.println("Some problem in redirecting the page");
//out.println(semValue);
}
out.println("</table></body></html>");
}
} - Assume that the information regarding the marks for all the subjects of a student in the last exam are available in a database, Develop a Servlet which takes the enrollment number of a student as a request parameter and displays the marksheet for the student.
//----------
HTML FILE ::
------------
<html>
<head>
<title>Untitled Document</title>
</head>
<script language="javascript">
function validate()
{
//alert(document.getElementById(1).value);
if(document.getElementById(1).value=="")
{
alert("Pleas enter enrollment number");
return false;
}
return true;
}
</script>
<body>
<table width="20%">
<form action="http://localhost:8080/GTU/Ex16" method="get" onSubmit="return validate();">
<tr>
<td>Enter Enrollment Number</td>
</tr>
<tr>
<td><input type="text" name="enum" id=1></td>
</tr>
<tr>
<td><input type="submit"></td>
</tr>
</form>
</table>
</body>
</html>
Servlet File…
-------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class Ex16 extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<html><body>");
String eNum=req.getParameter("enum");
/*out.println("<h2>"+eNum+"</h2>");
String myQuery="SELECT* FROM stud";
out.println("<h2>"+myQuery+"</h2>");
*/
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("JDBC:ODBC:DATASOURCE");
Statement s=con.createStatement();
s.executeQuery("Select * from result where EnrollNum='"+eNum+"'");
ResultSet rs=s.getResultSet();
rs.next();
if(rs!=null)
{
out.println("<table border=1>");
out.println("<tr><td>Roll No</td><td>"+rs.getString(2)+"</td></tr>");
out.println("<tr><td>Name</td><td>"+rs.getString(3)+"</td></tr>");
out.println("<tr><td>Subject1</td><td>"+rs.getString(4)+"</td></tr>");
out.println("<tr><td> Subject2</td><td>"+rs.getString(5)+"</td></tr>");
out.println("<tr><td> Subject3</td><td>"+rs.getString(6)+"</td></tr>");
out.println("<tr><td> Subject4</td><td>"+rs.getString(7)+"</td></tr>");
out.println("<tr><td>spi</td><td>"+rs.getString(8)+"</td></tr>");
out.println("</table>");
}
out.println("</body></html>");
}catch (Exception err) {
System.out.println( "Error: " + err );
}
}
} - Devlop A Servlet to which looks for cookies for username and Password, and forword to a home.jsp in case the cookies are valid and forword to a login.jsp in case the cookies are not found or the cookies are not valid.
//----------
Servlet ::
----------
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
class authorised extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
int cnt=0;
String userid="cuid";
String password="cpwd";
String uid=req.getParameter("txtuid");
String pwd=req.getParameter("txtpwd");
Cookie[] cookies=req.getCookies();
if(cookies != null)
{
for(int i=0;i<cookies.length;i++)
{
if((userid.equals(cookies[i].getName())) &&(uid.equals(cookies[i].getValue())))
{
cnt++;
}
if((password.equals(cookies[i].getName())) &&(pwd.equals(cookies[i].getValue())))
{
cnt++;
}
}
if(cnt==2)
{
res.sendRedirect("http://localhost:8080/wtad/jspfiles/home.jsp");
}
else
{
Cookie uidc=new Cookie(userid,uid);
Cookie pwdc=new Cookie(password,pwd);
res.addCookie(uidc);
res.addCookie(pwdc);
res.sendRedirect("http://localhost:8080/wtad/jspfiles/login.jsp");
}
}
}
}
HOME.JSP ::
-----------
<html>
<head>
<title>Hello WElcome to the Home page</title>
</head>
<body>
Hello WElcome to the Home page
</body>
</html>
LOGIN.JSP ::
-------------
<!-- login file for session tracting program 18 -->
<html>
<head>
<title>
login page
</title>
</head>
<body>
<form name="frmlogin"action="http://localhost:8080/wtad/authorised" method="post"
<center><B>User Id :: </B> <input type="text" name="txtid"><br>
<B>PassWord :: </B><input type="password" nam="txtpwd"><br>
<input type="submit" name="cmdsend" value="Log In">
<input type="reset" name="cmdclear" value="Clear"></center>
</form>
</body>
</html> - Devlop a Servlet to authenticate a user,where the login id and password are available as request parameter.In case the authentication is successful,it should setup a new session and store the user’s information in the session before forwarding to home.jsp, which displays a user informations.
//----------
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
class authoriseds extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
int cnt=0;
String userid="suid";
String password="spwd";
String uid=req.getParameter("txtuid");
String pwd=req.getParameter("txtpwd");
HttpSession session = request.getSession(true);
if(uid.equals(session.getValue(userid)) && pwd.equals(session.getValue(password)))
{
res.sendRedirect("http://localhost:8080/wtad/jspfiles/home.jsp");
}
else
{
session.addValue(userid,uid);
session.addValue(password,pwd);
res.sendRedirect("http://localhost:8080/wtad/jspfiles/login.jsp");
}
}
} - Write a simple JSP pages to display a simple message.
//----------
<html>
<head>
<title> JSP PAGE WITH DISPLAY HELLO WORLD</title>
</head>
<body>
<%! String msg="Hello World..."; %>
<center><h3 color="pink"> <%= msg %> </h3>
</body>
</html> - Write A JSP Page, which uses the include directive to show its header and footer
//----------
<!-- Write A JSP Page, which uses the include directive to show its header and footer -->
<html>
<head>
<title> </title>
</head>
<body>
<table border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" id="header">
<%@ include file="header.jsp" %>
</td>
</tr>
<tr height="500">
<td>
</td>
</tr>
<tr>
<td align="right" id="footer">
<%@ include file="footer.jsp" %>
</td>
</tr>
</table>
</body>
</html>
HEADER.JSP ::
<html>
<head>
<title>
Header Page for Applictaion
</title>
</head>
<body>
<table>
<tr>
<td colspan=4> Appication Names </td>
</tr>
<tr>
<td>link1</td>
<td>link2</td>
<td>link3</td>
<td>link4</td>
</tr>
</table>
</body>
</html>
FOOTER.JSP ::
<html>
<body>
<table align="right" cellpadding="0" cellspacing="5" border="0">
<tr align="right">
<td>link1 | </td>
<td>link2 | </td>
<td>link3 | </td>
<td>link4 | </td>
</tr>
<tr align="right">
<td align="left" colspan="2"> Site Visited : </td>
<td colspan="8">© 2000-2011 Application</td>
</tr></table>
</body>
</html>
BACK | NEXT |
Prepared By:
Student of B. H. Gardi College of Eng. & Tech., MCA Department
(Nisarg Juthani, Kajal Savjani, Desai Paresh, Ramoliya Nilesh, Thanki Ravi.....)
No comments:
Post a Comment