Practical no. 1 a. Create a simple calculator application using servlet. index.html
Enter First Number
Enter Second Number
Select an Operation ADDTION SUBSTRACTION MULTIPLY DIVIDE
CalculatorServlet.java public void doPost(HttpServletRequest request, HttpServletResponse response) Logic-1 double n1 = Double.parseDouble(request.getParameter("txtN1")); double n2 = Double.parseDouble(request.getParameter("txtN2")); double result =0; String opr=request.getParameter("opr"); Logic-2 if(opr.equals("+")) result=n1+n2; if(opr.equals("-")) result=n1-n2; if(opr.equals("*")) result=n1*n2; if(opr.equals("/")) result=n1/n2; out.println("

Result = "+result); b. Create a servlet for a login page. If the username and password are correct then it says message “Hello ” else a message “login failed” index.html
Enter User ID
Enter Password
LoginServlet.java public void doPost(HttpServletRequest request, HttpServletResponse response) Logic-1 String uname = request.getParameter("txtId"); String upass = request.getParameter("txtPass"); Logic-2 if(uname.equals("admin") && upass.equals("12345")){ out.println(""); out.println("

Welcome !!! "+uname+"

"); } else{ out.println("");out.println("

Login Fail !!!

"); Practical no.2 Create a registration servlet in Java using JDBC. Accept the details such as Username, Password, Email, and Country from the user using HTML Form and store the registration details in the database. create database (LoginDB) create table user create web application file and add library and add jar/folder in Library index.html

Welcome to Registration page

Enter User Name
Enter Password
Enter Email
Enter Country
RegisterServlet.java public void doPost(HttpServletRequest request, HttpServletResponse response) Logic-1 String id = request.getParameter("txtUid"); String ps = request.getParameter("txtPass"); String em = request.getParameter("txtEmail"); String co = request.getParameter("txtCon"); Logic-2 Class.forName("com.mysql.jdbc.Driver"); Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/logindb","root","root123"); PreparedStatement pst = con.prepareStatement("insert into user values(?,?,?,?)"); pst.setString(1,id); pst.setString(2,ps); pst.setString(3,em); pst.setString(4,co); int row = pst.executeUpdate(); out.println("

"+row+ " Inserted Succesfullyyyyy"); Practical no.3 a. 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 Welcome Servlet else the user will stay on the index.html page and an error message will be displayed. index.html
Enter User ID
Enter Password
LoginServlet.java public void doPost(HttpServletRequest request, HttpServletResponse response) Logic-1 String uname = request.getParameter("txtId"); String upass = request.getParameter("txtPass"); Logic-2 if(uname.equals("admin") && upass.equals("servlet")){ RequestDispatcher rd = request.getRequestDispatcher("WelcomeServlet"); rd.forward(request, response); } else{ out.println(""); out.println("

Login Fail !!!

"); WelcomeServlet.java public void doPost(HttpServletRequest request, HttpServletResponse response) Logic-2 ke niche out.println("

!!! Welcome Servlet !!!

"); 2b. Create a servlet that uses Cookies to store the number of times a user has visited servlet. (in servlet page uncheck the box) and from ioexception delete the entire code FirstCookieServlet.java protected void doGet(HttpServletRequest request, HttpServletResponse response) Cookie firstCookie = new Cookie("myCookie", "FirstCookie"); firstCookie.setMaxAge(3600); response.addCookie(firstCookie); response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { out.println("" + firstCookie.getName() + " cookie is created which holds the value " + firstCookie.getValue() + ""); /* TODO output your page here. You may use following sample code. */ out.println(""); out.println(""); out.println(""); out.println("Servlet FirstCookieServlet"); out.println(""); out.println(""); out.println("

Servlet FirstCookieServlet at " + request.getContextPath() + "

"); out.println(""); out.println("");       }     } } Practical no-4 A.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. (in servlet page uncheck the box) and from ioexception delete the entire code DisplaySessionDetailsServlet.java protected void doGet(HttpServletRequest request, HttpServletResponse response) response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); HttpSession mySession = request.getSession(true); String heading; Integer counter = new Integer(0); try { if (mySession.isNew()) { heading = "This is the first time you are visiting this page."; } else { heading = "Welcome back to this page"; Integer oldCounter = (Integer)mySession.getAttribute("counter"); if (oldCounter != null) { counter = new Integer(oldCounter.intValue() + 1); } } mySession.setAttribute ("counter", counter); out.println("Displaying Session Details"); out.println("

" + heading + "


"); out.println("SESSION ID: " + mySession.getId() + "
"); out.println("Creation Time of the Session: " + new Date(mySession.getCreationTime()) + "
"); out.println("Time of Last Access: " + new Date(mySession.getLastAccessedTime()) + "

"); out.println("You have visited this page " + (++counter)); out.println((counter == 1) ? " time " : " times "); out.println(""); } finally { out.close();        }     } } B.Create a Servlet application to upload and download a file. (in servlet page uncheck the box) and from ioexception delete the entire code FileDownloadServlet.java protected void doGet(HttpServletRequest request, HttpServletResponse response) String fileToDownload = request.getParameter("filename"); System.err.println("Downloading file now..."); downloadFile(request, response, fileToDownload); } private void downloadFile(HttpServletRequest request, HttpServletResponse response, String fileName) throws ServletException, IOException { int lenght = 0; try (ServletOutputStream outputStream = response.getOutputStream()) { ServletContext context = getServletConfig().getServletContext(); response.setContentType((context.getMimeType(fileName) != null) ? context.getMimeType(fileName) : "application/pdf"); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName); InputStream inputStream = context.getResourceAsStream("/" + fileName); byte[] bytes = new byte[1024]; while((inputStream != null) && ((lenght = inputStream.read(bytes)) != -1)) { outputStream.write(bytes, 0, lenght); } outputStream.flush();        }     } } Practical no. 5 A. Develop Simple Servlet Question Answer Application. index.html
Q1.What is the color of apple?
red
blue
white
Q2.What is 2*16?
34
32
56
Q3.Who won the world cup of 2024?
WI
India
England
Q4.Who is the National Bird of Australia?
Ostrich
Emu
Kingfisher
quiz.java protected void doPost(HttpServletRequest request, HttpServletResponse response) Logic-1 int correct=0,incorrect=0; String a1=request.getParameter("q1"); String a2=request.getParameter("q2"); String a3=request.getParameter("q3"); String a4=request.getParameter("q4"); Logic-2 if(a1.equals("red")) { correct ++; } else { incorrect ++; } if(a2.equals("32")) { correct ++; } else { incorrect ++; } if(a3.equals("India")) { correct ++; } else { incorrect ++; } if(a4.equals("Emu")) { correct ++; } else { incorrect ++; } out.println("Correct:"+correct+"Incorrect:"+incorrect); B.Develop a simple JSP application to display values obtained from the use of intrinsic objects of various types. example.jsp

Use of Intrinsic Objects in JSP

Request Object

Query String <%=request.getQueryString() %>
Context Path <%=request.getContextPath() %>
Remote Host <%=request.getRemoteHost() %>

Response Object

Character Encoding Type <%=response.getCharacterEncoding() %>
Content Type <%=response.getContentType() %>
Locale <%=response.getLocale() %>

Session Object

ID <%=session.getId() %>
Creation Time <%=new java.util.Date(session.getCreationTime()) %>
Last Access Time<%=new java.util.Date(session.getLastAccessedTime()) %>
Practical no.6 A.Develop a simple JSP application to pass values from one page to another with validations.(Name-txt, age-txt, hobbies-checkbox, email-txt, gender-radio button). index.html
Enter Your Name
Enter Your Age
Select Hobbies Singing Reading Books Playing Football
Enter E-mail
Select Gender Male Female Other
Validation.jsp <%-- Document : Validate Created on : Sep 19, 2024, 10:46:48 AM Author : SHAILESH --%> <%@page contentType="text/html" pageEncoding="UTF-8" import="mypack.*"%> JSP Page

Validation Page

<%if (obj.validate()) { %> <% } else {%> <%}%> <%=obj.getError() %> successful.jsp <%-- Document : successful Created on : Sep 19, 2024, 10:53:05 AM Author : SHAILESH --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> JSP Page

Hello World!

CheckerBean.java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package mypack; import javax.ejb.Stateless; /** * * @author SHAILESH */ @Stateless public class CheckerBean { private String name, age, hob, email, gender, error; public CheckerBean(){error="";} public void setName(String n){name=n;} public void setAge(String a){age=a;} public void setHob(String h){hob=h;} public void setEmail(String e){email=e;} public void setGender(String g){gender=g;} public void setError(String e){error=e;} public String getName(){return name;} public String getAge(){return age;} public String getHob(){return hob;} public String getEmail(){return email;} public String getGender(){return gender;} public String getError(){return error;} public boolean validate(){ boolean res=true; if(name.trim().equals("")) {error+="
Enter First Name";res=false;} if(age.length() > 2 ) {error+="
Age Invalid";res=false;} return res; } } B.Create a Currency Converter application using EJB. index.html
Enter Amount
Select Conversion Type Rupees to Dollar Dollor to Rupees
CCServlet.java @EJB CCBean obj; protected void doGet(HttpServletRequest request, HttpServletResponse response) Logic-1 double amt = Double.parseDouble(request.getParameter("amt")); Logic-2 if(request.getParameter("type").equals("r2d")) { out.println("

"+amt+ " Rupees = "+obj.r2Dollor(amt)+" Dollors

"); } if(request.getParameter("type").equals("d2r")) { out.println("

"+amt+ " Dollors = "+obj.d2Rupees(amt)+" Rupees

"); } CCBean.java @Stateless public class CCBean { public CCBean(){} public double r2Dollor(double r){ return r/65.65; } public double d2Rupees(double d){ return d*65.65; } } Practical no. 7 Create a registration and login JSP application to register the user based on username and password using JDBC. Register.html

New User Registration Page

Enter User Name
Enter Password
Re-Enter Password
Enter Email
Enter Country Name
Register.jsp <%@page contentType="text/html" import="java.sql.*"%>

Registration JSP Page

<% String uname=request.getParameter("txtName"); String pass1 = request.getParameter("txtPass1"); String pass2 = request.getParameter("txtPass2"); String email = request.getParameter("txtEmail"); String ctry = request.getParameter("txtCon"); if(pass1.equals(pass2)){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/logindb","root","root123"); PreparedStatement stmt = con.prepareStatement("insert into user values (?,?,?,?)"); stmt.setString(1, uname); stmt.setString(2, pass1); stmt.setString(3, email); stmt.setString(4, ctry); int row = stmt.executeUpdate(); if(row==1) { out.println("Registration Successful"); } else { out.println("Registration FFFFFAAAIIILLLL !!!!"); %> <% } }catch(Exception e){out.println(e);} } else { out.println("

Password Mismatch

"); %> <% } %> Practical no. 8 Create a registration and login JSP application to authenticate the user based on username and password using JDBC Login.html

Login Page

Enter User Name
Enter Password
Login.jsp <%@page contentType="text/html" import="java.sql.*"%>

Registration JSP Page

<% String uname=request.getParameter("txtName"); String pass = request.getParameter("txtPass"); try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/logindb","root","root123"); PreparedStatement stmt = con.prepareStatement("select password from user where username=?"); stmt.setString(1, uname); ResultSet rs = stmt.executeQuery(); if(rs.next()){ if(pass.equals(rs.getString(1))) { out.println("

~~~ LOGIN SUCCESSFULLL ~~~

"); } } else{ out.println("

User Name not exist !!!!!

"); %> <% } }catch(Exception e){out.println(e);} %>