Lab Manual
M.C.A. – 4th Semester
Submitted By : Paresh Chavda
Practical List
11)
Write a Servlet to display
“Hello World” on browser.
import
java.io.IOException;
import
java.io.PrintWriter;
import
java.util.ResourceBundle;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
public class
Hello extends HttpServlet {
private static final long serialVersionUID
= 1L;
@Override
public void doGet(HttpServletRequest
request,
HttpServletResponse
response)
throws IOException, ServletException
{
ResourceBundle rb =
ResourceBundle.getBundle("LocalStrings",request.getLocale());
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
String title =
rb.getString("helloworld.title");
out.println("<title>" +
title + "</title>");
out.println("</head>");
out.println("<body
bgcolor=\"white\">");
out.println("<a
href=\"../helloworld.html\">");
out.println("<img
src=\"../images/code.gif\" height=24 " +
"width=24 align=right
border=0 alt=\"view code\"></a>");
out.println("<a
href=\"../index.html\">");
out.println("<img
src=\"../images/return.gif\" height=24 " +
"width=24 align=right
border=0 alt=\"return\"></a>");
out.println("<h1>" +
title + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
* Xml File
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
Output
22) 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 headerjwt extends HttpServlet {
public void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String
title="header Title for jwt";
out.println("<HTML>\n"
+
"<HEAD><TITLE>"
+ title +" </TITLE></HEAD>\n" +
"<BODY
BGCOLOR=\"#FDFSE6\">\n" +
"<H1
align=\"CENTER\">" + title +"</H1>\n" +
"<B>
Request method:</B>" +
request.getMethod()
+"<BR>\n" +
"<B>
Request URI:</B>"+
request.getRequestURL()+
"<BR>\n"+
"<B>
Request Protocol:</B> "+
request.getProtocol()+"<BR>\n"+
"<TABLE
BORDER=1 ALIGN=\"CENTER\">\n"+
"<TR
BGCOLOR=\"#FFAD00\">\n"+
"<TH>Header
Name</TH>Header value</TR>");
Enumeration
headerNames=request.getHeaderNames();
//Enumeration
headerEnumeration = headers.getAllHeaders();
while(headerNames.hasMoreElements())
{
String
headerName=(String)headerNames.nextElement();
out.println("<TR><TD>"+headerName);
out.println("</TD></TR>"+request.getHeader(headerName));
}
out.println("</TABLE>\n</BODY></HTML>");
}
}
Output
33) Write a Servlet to display
parameters available on request.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class Gtu3 extends HttpServlet {
public void
doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException,
ServletException
{
response.setContentType("text/html");
PrintWriter out
= response.getWriter();
//out.println("<br><b>User:</b>:"+
request.getParameter("usr")+"<br><b>Password</b>:"+
request.getParameter("pwd")+"");
out.println("<html><head><title>Write
a Servlet to display parameters available on request.
.</title></head><body><center><h1>write a Servlet
to display parameters available on
request.</h1></center><br><br>");
//display
haeder name and value in table format
out.println("<table
border=1>");
out.println("<tr><th>Parameter
name</th><th>parameter values</th></tr>");
Enumeration
e=request.getParameterNames();
while(e.hasMoreElements())
{
String
header_name=(String) e.nextElement();
String
header_value=(String)request.getParameter(header_name);
out.println("<tr><td><b>"+header_name+"</b></td><td>"+header_value+"</td></tr>");
}
out.println("</table>");
out.println("</body></html>");
}
}
44) Write a Servlet to display
all the attributes available from request and
context.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class gtu4 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out
= response.getWriter();
out.println("<html><head><title>Write
a Servlet to display all the
attributes available from request and
context.</title></head>");
out.println("<table border=1>");
out.println("<tr><th>Request
Context</th><th>Parameter
values</th></tr>");
out.println("<tr><td><b>"+"Local
Name:getLocalName()"
+"</b></td><td>"+request.getLocalName()+"</td></tr>");
out.println("<tr><td><b>"+"Local
Port:getLocalPort()"
+"</b></td><td>"+request.getLocalPort()+"</td></tr>");
//out.println("<tr><td><b>"+"Protocol:getLocalProtocol()"
+"</b></td><td>"+request.getLocalProtocol()+"</td></tr>");
out.println("<tr><td><b>"+"Remote
Address:getLocalAddr()"
+"</b></td><td>"+request.getLocalAddr()+"</td></tr>");
out.println("<tr><td><b>"+"Server
Name :getServerName()"
+"</b></td><td>"+request.getServerName()+"</td></tr>");
out.println("<tr><td><b>"+"Server
Port :getServerPort()"
+"</b></td><td>"+request.getServerPort()+"</td></tr>");
out.println("<tr><td><b>"+"Char
Encoding:getCharacterEncoding()"
+"</b></td><td>"+request.getCharacterEncoding()+"</td></tr>");
out.println("<tr><td><b>"+"Content
Type:getContentType()"
+"</b></td><td>"+request.getContentType()+"</td></tr>");
out.println("<tr><td><b>"+"Context
Length:getContentLength()"
+"</b></td><td>"+request.getContentLength()+"</td></tr>");
out.println("</table>");
out.println("</body></html>");
}
}
Web.xml
<servlet>
<servlet-name>gtu4</servlet-name>
<servlet-class>gtu4</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>gtu4</servlet-name>
<url-pattern>/gtu4</url-pattern>
</servlet-mapping>
Output:
5. 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 java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class q5 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 gtu Program
3</h1>");
}
}
}
Web.xml
<servlet>
<servlet-name>q5</servlet-name>
<servlet-class>q5</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>q5</servlet-name>
<url-pattern>/q5</url-pattern>
</servlet-mapping>
output:
6) 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).
gtu6.html
<html>
<body>
<h2>
GTU MCA SEM - 4 SYLLABUS LIST
</h2>
<form
action="http://localhost:8080/paresh/gtu6" method="GET">
<table border=1>
<tr>
<td>
Your
Name:
</td>
<input
type="text" name="name">
</tr>
<tr>
<td>
select
pdf:
</td>
<td>
<select
name="sem">
<option
value="mca-1.pdf"> mca-1</option>
<option
value="mca-2.pdf"> mca-2</option>
<option
value="mca-3.pdf"> mca-3</option>
</select>
</td>
</tr>
<colspan=2
align="center">
<input
type ="submit" value ="show">
</table>
</form>
</body>
</html>
OR
OR
<html>
<body>
<h2>GTU
6</h2>
<table>
<tr>
<td>Your Subject
Code:</td>
<td><input
type="text" name="name"></td>
</tr>
<tr>
<td>
Select PDF :
</td>
<td>
<select
name="sem">
<option
value="CN.pdf">4649301</option>
<option
value="stats.pdf">4649302</option>
<option
value="mc.pdf">4649303</option>
<option
value="PROJECT.pdf">4649304</option>
<option value="JWT.pdf">4649305</option>
</select>
</td>
</tr>
<td
colspan="2" align="center">
<input
type="submit" value="Select File">
</td>
</table>
</form>
</body>
</html>
gtu6.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class gtu6 extends HttpServlet {
private static
final long serialVersionUID = 1L;
@Override
public void
doGet(HttpServletRequest req,
HttpServletResponse res)
throws
IOException, ServletException
{
res.setContentType("application/pdf");
PrintWriter out
= res.getWriter();
String
name=req.getParameter("sem");
res.sendRedirect(name);
}
}
web.xml
<servlet>
<servlet-name>gtu6</servlet-name>
<servlet-class>gtu6</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>gtu6</servlet-name>
<url-pattern>/gtu6</url-pattern>
</servlet-mapping>
output:
Gtu66.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class gtu66 extends HttpServlet {
private static
final long serialVersionUID = 1L;
@Override
public void
doGet(HttpServletRequest req,
HttpServletResponse res)
throws
IOException, ServletException
{
res.setContentType("text/html");
PrintWriter out = res.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
IOException, ServletException
{
String
st=request.getParameter("syll");
response.setContentType("application/pdf");
if(st.equals("mca1"))
{
response.sendRedirect("./mca-1.pdf");
}
else
if(st.equals("mca2"))
{
response.sendRedirect("./mca-2.pdf");
}
else
if(st.equals("mca3"))
{
response.sendRedirect("./mca-3.pdf");
}
}
}
16) GTU jwt Practical 8
<html>
<head>
<title>GTU jwt Practical 7</title>
</head>
<body>
<h2 align="center"> GTU jwt Practical 8 : </h2>
<table>
<tr>
<th>Student Result</th>
</tr>
<tr>
<td>
<form action="../gtu7dbs" method="get">
Enrollment Number :
<input type="text" name="par1" value="">
<input type="submit" value="submit">
</form>
</td>
</tr>
</table>
</body>
</html>
<head>
<title>GTU jwt Practical 7</title>
</head>
<body>
<h2 align="center"> GTU jwt Practical 8 : </h2>
<table>
<tr>
<th>Student Result</th>
</tr>
<tr>
<td>
<form action="../gtu7dbs" method="get">
Enrollment Number :
<input type="text" name="par1" value="">
<input type="submit" value="submit">
</form>
</td>
</tr>
</table>
</body>
</html>
gtu7dbs.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
public class prg7 extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException,ServletException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String name="";
try
{
//need restart after adding exts i think so restart ur lappy
//Class.forName("com.mysql.cj.jdbc.Driver");what is ur email id???
out.println("<h1 align='center'>Your Result Is:</h1>");
Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/student_marks","root","");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from stud where eno ="+req.getParameter("enrolment"));
// ResultSet rs=stmt.executeQuery("select * from stud");
out.println("<table border=1 align='center'>");
out.println("<tr><th>JWT</th><th>SM</th><th>MC</th></tr>");
while(rs.next())
{
//name=""+rs.getString(1);
out.println("<tr><td>"+rs.getInt(3)+"</td><td>"+rs.getInt(4)+"</td><td>"+rs.getInt(5)+"</td></tr>");
name=rs.getString(2);
}
out.println("</table>");
out.println("<h4 align='center'>Your Enrolment No:"+req.getParameter("enrolment")+"</h4>");
out.println("<h4 align='center'>Your Name:"+name+"</h4>");
con.close();
}
catch(Exception ex)
{
out.println("Exception:-"+ex);
}
finally
{
out.close();
}
}
}
OR
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import javax.sql.*;
public class gtu7dbs extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// JDBC driver name and database URL
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL="jdbc:mysql://localhost/gtu";
String USER = "root";
String PASS = "";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Database Result";
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 = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + title + "</h1>\n");
try {
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Execute SQL query
Statement stmt = conn.createStatement();
String enr=request.getParameter("txtpar1");
String sql = "SELECT * FROM student where enrollmentno='"+enr+"'";
ResultSet rs = stmt.executeQuery(sql);
// Extract data from result set
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("enrollmentno");
String studnm= rs.getString("student");
int m1 = rs.getInt("mark-1");
int m2 = rs.getInt("mark-2");
int m3 = rs.getInt("mark-3");
int m4 = rs.getInt("mark-4");
int m5 = rs.getInt("mark-5");
//Display values
out.println("ID: " + id + "<br>");
out.println(" Student Name : " + studnm + "<br>");
out.println("Marks-1: " + m1 + "<br>");
out.println(" Marks-2: " + m2 + "<br>");
out.println(" Marks-3: " + m3 + "<br>");
out.println(" Marks-4: " + m4 + "<br>");
out.println(" Marks-5: " + m5 + "<br>");
}
out.println("</body></html>");
rs.close();
stmt.close();
conn.close();
} catch(SQLException se) {
se.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} finally {
}
}
}
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
public class prg7 extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException,ServletException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String name="";
try
{
//need restart after adding exts i think so restart ur lappy
//Class.forName("com.mysql.cj.jdbc.Driver");what is ur email id???
out.println("<h1 align='center'>Your Result Is:</h1>");
Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/student_marks","root","");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from stud where eno ="+req.getParameter("enrolment"));
// ResultSet rs=stmt.executeQuery("select * from stud");
out.println("<table border=1 align='center'>");
out.println("<tr><th>JWT</th><th>SM</th><th>MC</th></tr>");
while(rs.next())
{
//name=""+rs.getString(1);
out.println("<tr><td>"+rs.getInt(3)+"</td><td>"+rs.getInt(4)+"</td><td>"+rs.getInt(5)+"</td></tr>");
name=rs.getString(2);
}
out.println("</table>");
out.println("<h4 align='center'>Your Enrolment No:"+req.getParameter("enrolment")+"</h4>");
out.println("<h4 align='center'>Your Name:"+name+"</h4>");
con.close();
}
catch(Exception ex)
{
out.println("Exception:-"+ex);
}
finally
{
out.close();
}
}
}
OR
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import javax.sql.*;
public class gtu7dbs extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// JDBC driver name and database URL
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL="jdbc:mysql://localhost/gtu";
String USER = "root";
String PASS = "";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Database Result";
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 = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + title + "</h1>\n");
try {
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Execute SQL query
Statement stmt = conn.createStatement();
String enr=request.getParameter("txtpar1");
String sql = "SELECT * FROM student where enrollmentno='"+enr+"'";
ResultSet rs = stmt.executeQuery(sql);
// Extract data from result set
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("enrollmentno");
String studnm= rs.getString("student");
int m1 = rs.getInt("mark-1");
int m2 = rs.getInt("mark-2");
int m3 = rs.getInt("mark-3");
int m4 = rs.getInt("mark-4");
int m5 = rs.getInt("mark-5");
//Display values
out.println("ID: " + id + "<br>");
out.println(" Student Name : " + studnm + "<br>");
out.println("Marks-1: " + m1 + "<br>");
out.println(" Marks-2: " + m2 + "<br>");
out.println(" Marks-3: " + m3 + "<br>");
out.println(" Marks-4: " + m4 + "<br>");
out.println(" Marks-5: " + m5 + "<br>");
}
out.println("</body></html>");
rs.close();
stmt.close();
conn.close();
} catch(SQLException se) {
se.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} finally {
}
}
}
Web.xml
<servlet>
<servlet-name>gtu7dbs</servlet-name>
<servlet-class>gtu7dbs</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>gtu7dbs</servlet-name>
<url-pattern>/gtu7dbs</url-pattern>
</servlet-mapping>
8) Develop a Servlet which looks for cookies for username and password, and forwards to a home.jsp in case the cookies are valid and forwards to login.jsp, in case the cookies are not found or the cookies are not valid.
File Name : index.jsp
File Location : Tomcat_Apache\webapps\gtu17\
<html> <head> <title>GTU JWT Practical 8 :</title> </head> <body> <h2 align="center"> GTU JWT Practical 8 : </h2> <form action="/gtu8/LoginVerificationUsingCookies" method="get"> <table> <tr><th colspan="2">Login</th></tr> <tr><td>Username : </td><td> <input type="text" name="par1" value=""> </td></tr> <tr><td>Password : </td><td> <input type="password" name="par2" value=""> </td></tr> <tr><td colspan="2"><input type="submit" value="Login"></td></tr> </table> </form> </body> </html>
File Name : home.jsp
File Location : Tomcat_Apache\webapps\gtu17\
<html> <head> <title>GTU JWT Practical 8</title> </head> <body> <h2 align="center"> GTU JWT Practical 8 : Develop a Servlet which looks for cookies for username and password, and forwards to a home.jsp in case the cookies are valid and forwards to login.jsp, in case the cookies are not found or the cookies are nto valid </h2> <br><br><center> <table> <tr> <th>Welcome to Home Page</th> </tr> </table> </center> </body> </html>
File Name : web.xml
File Location : Tomcat_Apache\webapps\gtu17\WEB-INF
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <display-name>GTU JWT Practical 8 </display-name> <description>ADevelop a Servlet which looks for cookies for username and password, and forwards to a home.jsp in case the cookies are valid and forwards to login.jsp, in case the cookies are not found or the cookies are nto valid </description> <servlet> <servlet-name>LoginVerificationUsingCookies</servlet-name> <servlet-class>LoginVerificationUsingCookies</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginVerificationUsingCookies</servlet-name> <url-pattern>/LoginVerificationUsingCookies</url-pattern> </servlet-mapping> </web-app>
File Name : LoginVerificationUsingCookies.java
File Location : Tomcat_Apache\webapps\gtu17\WEB-INF\classes
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class LoginVerificationUsingCookies extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { PrintWriter out=response.getWriter(); response.setContentType("text/html"); out.println("<html><head><title>GTU WTAD Practical 8 : Develop a Servlet which looks for cookies for username and password, and forwards to a home.jsp in case the cookies are valid and forwards to login.jsp, in case the cookies are not found or the cookies are nto valid </title></head><body></h1></center><br><br>"); String s1 = request.getParameter("par1"); String s2 = request.getParameter("par2"); Cookie[] c = request.getCookies(); boolean user=false,pass=false; for(int i=0;i<c.length;i++) { Cookie t = c[i]; if(t.getName().equals("username") && t.getValue().equals(s1) ) { user=true; } if(t.getName().equals("password") && t.getValue().equals(s2) ) { pass=true; } } if(user==true && pass==true) { response.sendRedirect("home.jsp"); } else { Cookie c1 = new Cookie("username",s1); Cookie c2 = new Cookie("password",s2); c1.setMaxAge(60*60*24*7); c2.setMaxAge(60*60*24*7); response.addCookie(c1); response.addCookie(c2); response.sendRedirect("index.jsp"); } } }
Output (index page)
Output (home page after login)
good
ReplyDeletethank you bro...
ReplyDelete