Showing posts with label Servlet. Show all posts
Showing posts with label Servlet. Show all posts
example of preparedstatement in java
Create Table in MySql [ Employee ]
CREATE TABLE `wtad`.`Employee` (
`EmpId` INT( 4 ) NOT NULL ,
`Empname` VARCHAR( 15 ) NOT NULL ,
`Emp_desig` VARCHAR( 20 ) NOT NULL ,
`Emp_J_Date` VARCHAR( 20 ) NOT NULL ,
`Emp_Salary` INT( 8 ) NOT NULL
) ENGINE = MYISAM ;
Create Table in Oracle [ Employee ]
Example of requestdispatcher
index.jsp
<html>
<head>
<title>MySqlLogin</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:300px;
left:5in;
border-style:solid;
}
</style>
</head>
<head>
<title>MySqlLogin</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:300px;
left:5in;
border-style:solid;
}
</style>
</head>
Difference between GenericServlet and HttpServlet
GenericServlet:
- As name says it Generic in nature. Means GenericServlet is protocol-independent.
- import javax.servlet.servlet package
- super class of GenericServlet are java.lang.Object and it implements three interfaces Servlet, ServletConfig and java.io.Serializable
- To write a generic servlet, override the abstract service().
- GenericServlet lifecycle methods use ServletConfig interface.
- GenericServlet implements the log() from ServletContext interface.
- Prototype:
- public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.Serializable
- Example:
hello world servlet example
Step 1 : Create .java File using text editor
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ServletDemo extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML><HEAD><TITLE>WTAD</TITLE></HEAD> <body>");
String str="<h1>welcome to </h1>";
str=str+"<br>www.bipinrupadiya.blogspot.com";
out.println(str);
out.println("</body> </html>");
}
}
28. Develop a program to perform the database driven operation like insert, Delete, Update and select. To perform the above operations create one table named Employee.
Create Table in MySql [ Employee ]
CREATE TABLE `wtad`.`Employee` (
`EmpId` INT( 4 ) NOT NULL ,
`Empname` VARCHAR( 15 ) NOT NULL ,
`Emp_desig` VARCHAR( 20 ) NOT NULL ,
`Emp_J_Date` VARCHAR( 20 ) NOT NULL ,
`Emp_Salary` INT( 8 ) NOT NULL
) ENGINE = MYISAM ;
16. 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.
1. Create Database in MySql / Oracle / Access
You can choose any database. Here, I provide the solution to connect database like MySQL, Oracle, and Microsoft Access
2. Create Table IN MySQL
create table MARK (EnNo VARCHAR(10),Code VARCHAR(10),Name VARCHAR(10),Mark INT(3));
3. Create Table IN Oracle
3. Create Table IN Oracle
create table MARK (EnNo VARCHAR(10),Code VARCHAR(10),Name VARCHAR(10),Mark NUMBER (3));
4. Create Web Application in tomcat using following code
index.jsp
<HTML>
<HEAD>
<TITLE>Ex-16 : www.BipinRupadiya.blogspot.in</TITLE>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:400px;
left:5in;
border-style:solid;
}
</style>
</HEAD>
<BODY>
<div class="abc" id="f1">
<br>
<center>
<h3>
<FORM METHOD=POST ACTION="Ex16.com">
Enrollment No.: <input type=TEXT name = "txtEnNo" >
<BR><BR><input type="submit" value="Show Result">
</FORM>
</div>
</BODY>
</HTML>
Ex16.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import javax.servlet.annotation.WebServlet;
@WebServlet("/Ex16.com")
public class Ex16 extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String enNo = request.getParameter("txtEnNo");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try
{
//Connect To MySql
/* Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wtad","root","admin");
*/
//Connect To Oracle
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","scott","tiger");
//Connect To Access
/*
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:test","bipin","rupadiya");
*/
String sql = "select * from MARK where EnNo = ?";
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1, enNo);
ResultSet rs= ps.executeQuery();
out.println("<HTML><BODY bgcolor='#ababff'>");
out.println("<center><h1> Marksheet</h1>");
out.println("<TABLE BORDER=1 cellpadding='5px' width=50%");
out.println("<TR><TH colspan=3> Enrollment No.:"+enNo+"</TH></TR>");
out.println("<TR><TH>CODE </TH> <TH>NAME </TH> <TH> GRADE </TH></TR>");
if(rs == null)
{
out.println("<h1> Result Not Found... </h1>");
}
else
{
while(rs.next())
{
out.println("<TR>");
out.println("<TD>"+rs.getString(2)+"</TD>");
out.println("<TD>"+rs.getString(3)+"</TD>");
out.println("<TD>"+rs.getInt(4)+"</TD>");
out.println("</TR>");
}
out.println("<TR><TH colspan=3> <a href='index.jsp'>Home</a> </TH></TR>");
out.println("</TABLE></CENTER></BODY></HTML>");
rs.close();
ps.close();
con.close();
}
}
catch(SQLException e)
{
out.println(e);
}
catch(ClassNotFoundException e)
{
out.println(e);
}
catch(Exception e)
{
out.println(e);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGet(request, response);
}
}
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);
}
}
create GenericServlet
Step 1 : Create .java File using text editor
import javax.servlet.*;
import java.io.*;
import javax.servlet.annotation.WebServlet;
@WebServlet(
name="WTAD",
urlPatterns={"/Ex10","/index.html","/BipinRupadiya.blogspot.com"}
)
public class GenricServletDemo extends GenericServlet
{
public void service(ServletRequest request, ServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>WTAD</title></head> <body>");
String str="<h1>
welcome to </h1>
";
str=str+"<br />www.bipinrupadiya.blogspot.com";
out.println(str);
out.println("</body> </html>");
}
}
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.

