0% found this document useful (0 votes)
5 views9 pages

Java Sem 5 Final

The document outlines the creation of various Java servlet applications, including a simple calculator, login page, password validation, cookie usage, session management, and a question-answer application. It provides HTML form structures and corresponding Java servlet code to handle user inputs and responses. Additionally, it includes a JSP application for displaying implicit objects and an EJB-based currency converter application.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Java Sem 5 Final

The document outlines the creation of various Java servlet applications, including a simple calculator, login page, password validation, cookie usage, session management, and a question-answer application. It provides HTML form structures and corresponding Java servlet code to handle user inputs and responses. Additionally, it includes a JSP application for displaying implicit objects and an EJB-based currency converter application.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1A:Create a simple calculator application using servlet.

[Link]:
<html>
<body>
<form action="check" method="get">
Enter number 1: <input type="text" name="n1">
Enter number 2: <input type="text" name="n2">
Select List <select name="Arithmetic">
<option value="+"> + </option>
<option value="-"> - </option>
<option value="*"> * </option>
<option value="/"> / </option>
</select>
<input type="submit" value="Submit">
</form>
</body>
</html>
[Link]:
import [Link].*
import [Link].*;
public class check extends GenericServlet {
public void service(ServletRequest req, ServletResponse res)
throws IOException, ServletException
{
PrintWriter out=[Link]();
int a=[Link]([Link]("n1"));
int b=[Link]([Link]("n2"));
String s=[Link]("Arithmetic");
if([Link]("+")){
[Link]("Addition is "+(a+b));
}
else if([Link]("-")){
[Link]("Subtraction is "+(a-b));
}
else if([Link]("*")){
[Link]("Multipliation is "+(a*b));
}
else{
[Link]("Division is "+(a/b));
}}}
1B: Create a servlet for a login page. If the username and password are correct then it says
message “Hello <username>” else a message “Login failed”.
[Link]:
<html>
<body>
<form action="demo">
Enter UserName:<input type="text" name="n1"><br>
Enter Password:<input type="password" name="n2"><br>
<input type="submit" value="Submit"><br>
</form>
</body>
</html>
[Link]:
import [Link].*;
import [Link].*;
public class demo extends GenericServlet {
public void service(ServletRequest req,ServletResponse res)
throws ServletException, IOException {
PrintWriter out = [Link]();
String n1=[Link]("n1");
String n2=[Link]("n2");
if([Link]("ABC")&& [Link]("123"))
{
[Link]("Login Successful");
}
Else{
[Link]("Try again !!!");
}}}
2A: Using Request Dispatcher Interface create a Servlet which will validate the password entered
by the user, if the user has entered “Servlet” as password, then he will be forwarded to
[Link]:
<html> <body>
<form action="demo" method="get">
Enter Username:<input type="text" name="n1"><br>
Enter Password:<input type="password" name="n2"><br>
<input type="submit" value="Submit"><br>
</form>
</body> </html>
[Link]:
import [Link].*;
import [Link].*;
public class demo extends GenericServlet {
public void service(ServletRequest req, ServletResponse res)
throws IOException, ServletException
{
PrintWriter out=[Link]();
String n1=[Link]("n1");
String n2=[Link]("n2");
if([Link]("pass"))
{
[Link]("user",n1);
RequestDispatcher rd=[Link]("welcome");
[Link](req, res);
}
else {
[Link]("Password is invalid");
}}}
[Link]:
import [Link].*;
import [Link].*;
public class welcome extends GenericServlet {
public void service(ServletRequest req, ServletResponse res)
throws IOException, ServletException
{
String s =(String)[Link]("user");
PrintWriter out=[Link]();
[Link](s+" Aap ka swagat hai");
}}
2B: Create a servlet that uses Cookies to store the number of times a user has visited servlet.
[Link]:
<html> <body>
<form action="cookie" method="doGet">
<input type="submit" value="submit">
</form>
</body> </html>
[Link]:
package cookie;
import [Link].*;
import [Link].*;
import [Link].*;
public class cookie extends HttpServlet{
static int i=1;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException{
[Link]("text/html");
PrintWriter out=[Link]();
String k=[Link](i);
Cookie c=new Cookie("visit",k);
[Link](c);
int j=[Link]([Link]());
if(j==1){
[Link]("!!Welcome!! Now refresh your page");
}
else{
[Link]("You visited "+i+" times");
}
i++;
}}
2C: Create a servlet demonstrating the use of session creation and destruction. Also check
whether the user has visited this page first time or has visited earlier also using sessions.
[Link]
<html>
<body>
<form action="session" method="get">
Enter User Id:<input type="text" name="txtName"><br>
<input type="reset" value="Reset">
<input type="submit" value="Submit">
</form>
</body>
</html>
[Link]:
import [Link].*;
import [Link].*;
import [Link].*;
public class session extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
PrintWriter out=[Link]();
[Link]("<html><body>");
HttpSession hs=[Link](true);
if([Link]())
{
String name=[Link]("txtName");
[Link]("uname", name);
[Link]("visit","1");
[Link]("<h5>Welcome First Time</h5>");
}
else
{
[Link]("<h5>Welcome Again</h5>");
int visit=[Link]((String)[Link]("visit"))+1;
[Link]("<h5>You visited "+visit+" times</h5>");
[Link]("visit", visit);
}
[Link]("<h5>Your Seesion Id "+[Link]()+"</h5>");
[Link]("<h5>You Loged in at "+new [Link]([Link]())+"</h5>");
[Link]("<h5><a href=logoutservlet>Click to LOGOUT</a></h5>");
[Link]("</body>");
[Link]("</html>");
}}
[Link]:
package logoutservlet;
import [Link];
import [Link];
import [Link];
import [Link].*;
public class logoutservlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
PrintWriter out=[Link]();
[Link]("<html>");
[Link]("<body>");
HttpSession hs=[Link]();
if(hs!=null);
{
[Link]();
}
[Link]("<h1>You are logout now...</h1>");
[Link]("</body>");
[Link]("</html>");
}}
3B: Develop Simple Servlet Question Answer Application.
[Link]:
<html>
<body>
<form method="post" action="check">
<h1>My paper</h1>
Q. 1) Which is a valid keyword in java?<br>
<input type="radio" name="r1" value ="int"/>interface
<input type="radio" name="r1" value ="F"/>Float
<input type="radio" name="r1" value ="s"/>string
<input type="radio" name="r1" value ="all"/>All of the above<br><br>
Q. 2) What is sent to the user via HTTP, invoked using the HTTP protocol on the user's computer, and
run on the user's computer as an application?<br>
<input type="radio" name="r2" value="app"/>A Java application
<input type="radio" name="r2" value="applet"/>A Java applet
<input type="radio" name="r2" value="servlet"/>A Java servlet
<input type="radio" name="r2" value="none"/>None of the above <br><br>
Q. 3) JDBC stands for:<br>
<input type="radio" name="r3" value="jdbc"/>Java Database Connectivity
<input type="radio" name="r3" value="jdbc1"/>Java Database Components
<input type="radio" name="r3" value="jdbc2"/>Java Database Control
<input type="radio" name="r3" value="none"/>None of the above <br><br>
Q. 4) What is bytecode?<br>
<input type="radio" name="r4" value="msc"/>Machine-specific code
<input type="radio" name="r4" value="jcn"/>Java code
<input type="radio" name="r4" value="mic"/>Machine-independent code
<input type="radio" name="r4" value="none"/>None of the above <br><br>
<input type="submit" value="Display Result"/>
<input type="reset" value="Reset Values"/>
</form>
</body>
</html>
[Link]:
import [Link].*;
import [Link].*;
public class check extends GenericServlet{
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException{
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
try{ [Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet page</title>");
[Link]("</head>");
[Link]("<body>");
String a1,a2,a3,a4; a1=[Link]("r1");
a2=[Link]("r2");
a3=[Link]("r3");
a4=[Link]("r4");
if(([Link]("int"))&&([Link]("app" ))&&([Link]("jdbc"))&&([Link]("mic"))){
[Link]("You are Pass");}
else{
[Link]("Better luck Next time");
}
[Link]("</body>");
[Link]("</html>");
}
finally{ [Link](); }}}
4A: Develop a simple JSP application to display values obtained from the use of intrinsic objects of
various types.
[Link]:
<html>
<body>
<h1>Use of implicit object in jsp</h1>
<h1>Request Object</h1>
Context Path <%=[Link]()%><br>
Remote Host <%=[Link]()%><br>
<h1>Response Object</h1>
Character Encoding Type <%=[Link]()%><br>
Content Type <%=[Link]()%><br>
<h1>Session Object</h1>
ID <%=[Link]()%><br>
Creation Time <%=new [Link]([Link]())%><br>
Access Time <%=new [Link]([Link]())%>
</body>
</html>
6A: Create a Currency Converter application using EJB.
[Link]:
<html>
<body>
<form action="CCServlet" >
Enter Amount:<input type="text" name="amt"><br>
Select Conversion Type:<input type="radio" name="type" value="r2d" checked>
Rupees to Dollar <input type="radio" name="type" value="d2r" >
Dollar to Rupees<br> <input type="reset" value="RESET">
<input type="submit" value="CONVERT" >
</form>
</body>
</html>
[Link]:
package mybean;
import [Link]; @Stateless
public class CCBean
{
public CCBean(){}
public double r2Dollar(double r)
{
return r/65.65;
}
public double d2Rupees(double d)
{
return d*65.65;
}}
[Link]:
import [Link];
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];
public class CCServlet extends HttpServlet {
@EJB CCBean obj;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
double amt = [Link]([Link]("amt"));
if([Link]("type").equals("r2d")) {
[Link]("<h1>"+amt+ " Rupees = "+obj.r2Dollar(amt)+" Dollars</h1>");
}
if([Link]("type").equals("d2r"))
{
[Link]("<h1>"+amt+ " Dollars = "+obj.d2Rupees(amt)+" Rupees</h1>");
}}}

You might also like