Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Create a servlet for a login page. If the username and password are correct then it says message “Hello ” else a message “login failed” D
- Input.html
- <body>
- <form name=f1 method=get action="Login" >
- Enter UserId <input type="text" name="t1" ><br>
- Enter Password<input type="text" name="t2" ><br>
- <input type="submit" value="OK" >
- </form>
- </body>
- Login.java
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class Login extends HttpServlet
- {
- public void doGet(HttpServletRequest req,
- HttpServletResponse res) throws ServletException,IOException
- {
- PrintWriter pw=res.getWriter();
- String u= req.getParameter("t1");
- String p= req.getParameter("t2");
- if(u.equals("tyit") && p.equals("java"))
- {
- pw.println("Hello "+u);
- } else
- {
- pw.println("Login failed");
- }
- }
- }
- 2. Create a JSP application to demonstrate the use of JSTL. D
- index.jsp
- <html>
- <head>
- <title>Index Page</title>
- </head>
- <body>
- <form action="jstlDemo.jsp" method="post">
- First Name:<input type="text" name="fname"/><br/>
- Last Name:<input type="text" name="lname"/><br/>
- <input type="Submit" value="Check JSTL Demo"/>
- </form>
- </body>
- </html>
- jstlDemo.jsp
- first download jstl from:https://www.javatpoint.com/jstl
- save it in a loacatable drive
- copy paste it in WEB-INF folder &
- Add JAR file of jstl in Libraries
- code:
- <%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
- <html>
- <head>
- <title>Welcome Page</title>
- </head>
- <body>
- First Name:<b> <c:out value="${param.fname}"></c:out></b><br>
- Last name <b> <c:out value="${param.lname}"></c:out></b><br>
- Use of if statement
- <br>
- <c:set var="mycount" value="25"/>
- <c:if test="${mycount == 25}">
- <b> <c:out value="Your count is 25"/> </b>
- </c:if>
- <br><br>
- Use of for Each statement
- <br>
- <c:forEach var="count" begin="101" end="105">
- <b> <c:out value="${count}"/></b>
- </c:forEach>
- <br><br>
- Exception catching Example
- <p>
- <c:catch var="myException">
- <%int number =10/0 ;%>
- </c:catch>
- <b> The Exception is ${myException}</b>
- </p>
- </body>
- </html>
- 3. Develop a simple JSP application to display values obtained from the use of implicit objects of various types D
- input.html
- <body>
- <form action="index.jsp">
- <input type="text" name="t1">
- <input type="submit" value =”ok”>
- </form>
- </body>
- index.jsp
- <%
- String n=request.getParameter("t1");
- out.println(n);
- out.println(request.getMethod()+"<br/>");
- out.println(request.getRequestURL()+"<br/>");
- out.println(request.getQueryString()+"<br/>");
- response.setContentType("text/html");
- out.println(response.getCharacterEncoding()+"<br/>");
- out.println(response.getContentType()+"<br>");
- out.println(session.getId()+"<br/>");
- out.println(session.getCreationTime()+"<br/>");
- out.println(session.getLastAccessedTime()+"<br/>");
- %>
- 4. Develop simple application to demonstrate accessing Session Bean using EJB
- 5. Write a servlet code to store the biodata details of a user into the database. (Name, age, address, hobbies, gender, Qualification).
- Index.jsp
- <html>
- <body>
- <form action="Bio" method="post">
- Enter name<input type="text" name="txt1">
- Enter age<input type="date" name="date">
- Enter Address<input type="text" name="txt2">
- Select Hobbies
- <input type="checkbox" name="hob" value="Singing">Singing
- <input type="checkbox" name="hob" value="Dancing">Dancing
- <input type="checkbox" name="hob" value="Reading">Reading
- Select Gender
- <input type="checkbox" name="g1" value="male">Male
- <input type="checkbox" name="g1" value="female">Female
- <input type="checkbox" name="g1" value="Other">Other
- Enter Qualification<input type="text" name="txt7">
- <input type="submit" value="submit">
- </form>
- </html>
- Bio.java
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class Bio extends HttpServlet {
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html");
- PrintWriter out=response.getWriter();
- String n1=request.getParameter("txt1");
- String n2=request.getParameter("date");
- String n3=request.getParameter("txt2");
- String n4=request.getParameter("hob");
- String n5=request.getParameter("g1");
- String n6=request.getParameter("txt7");
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/logindb", "root", "root");
- PreparedStatement ps = con.prepareStatement("insert into bio values(?,?,?,?,?,?)");
- ps.setString(1, n1);
- ps.setString(2, n2);
- ps.setString(3, n3);
- ps.setString(4, n4);
- ps.setString(5, n5);
- ps.setString(6, n6);
- int i = ps.executeUpdate();
- if (i > 0);
- out.print("You are successfully registered...");
- } catch (Exception e2) {
- System.out.println(e2);
- }
- out.close();
- }
- }
- 6. Develop simple EJB application to demonstrate Servlet Hit count using Singleton Session Beans. ///wrong
- 7. Develop a JSP application to authenticate user login with database. D
- ADD JAR FILE OF MYSQL
- 1. Go to services – database
- 2. right click on database - new connection
- in driver select Mysql Connector(connector/j driver).
- 3. Click on Next Give database name and root password.
- click on Test connection - next -finish
- On Mysql
- create database db1;
- use db1;
- create table user(username varchar(20),password varchar(20),emailid varchar(100));
- select * from user;
- demo.html
- <body>
- <a href="input.html">Register new user</a>
- <a href="input1.html">Login</a>
- </body>
- input.html
- <body>
- <form action="insert.jsp">
- User name<input type=text name=t1>
- Password<input type=password name=t2>
- Email ID<input type=text name=t3>
- <input type=submit value=ok>
- </form>
- </body>
- insert.jsp
- <body>
- <%@ page import="java.sql.*"%>
- <%! String u,p,e;%>
- <%
- u=request.getParameter("t1");
- p=request.getParameter("t2");
- e=request.getParameter("t3");
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con=DriverManager.getConnection("jdbc:mysql://localhost/db1","root","abc");
- PreparedStatement pst=con.prepareStatement("insert into user values(?,?,?)");
- pst.setString(1,u);
- pst.setString(2,p);
- pst.setString(3,e);
- pst.execute();
- con.close();
- out.println("Data inserted successfully");
- }
- catch(Exception e)
- {
- out.println(e);
- }
- %>
- </body>
- input1.html
- <body>
- <form action="verify.jsp">
- User name<input type=text name=t1>
- Password<input type=password name=t2>
- <input type=submit value=Login>
- </form>
- </body>
- verify.jsp
- <%@ page import="java.sql.*"%>
- <%! String u,p;%>
- <%
- boolean f=false;
- u=request.getParameter("t1");
- p=request.getParameter("t2");
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con=DriverManager.getConnection("jdbc:mysql://localhost/db1","root","abc");
- Statement st=con.createStatement();
- ResultSet rs=st.executeQuery("select * from user");
- while(rs.next())
- {
- if(rs.getString("username").equals(u) && rs.getString("password").equals(p))
- {
- f=true;
- out.println("Login successful");
- break;
- }
- }
- if(f==false)
- {
- out.println("Invalid user");
- }
- }
- catch(Exception e)
- {
- out.println(e);
- }
- %>
- 8. Create a JSP page to demonstrate the use of Expression language.
- Input.html
- <body>
- <form action="demo.jsp">
- Enter name <input type="text" name="t1">
- <input type="submit" value="ok">
- </form>
- </body>
- Demo.jsp
- <body>
- WELCOME ,${param.t1} <br>
- The sum is ${1+9} <br>
- The mul is ${1*9} <br>
- The sub is ${10-9} <br>
- The div is ${19/9}
- </body>
- 9. Create a Servlet application to upload and download a file.
- File location should be as per pc
- Upload file example
- Input.html
- <body>
- <form action="Upload" method="post" enctype="multipart/form-data">
- Select File to Upload:<input type="file" name="t1" >
- <input type="submit" value="ok">
- </form>
- </body>
- Upload.java
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.annotation.*;
- import javax.servlet.http.*;
- @MultipartConfig
- public class Upload extends HttpServlet
- {
- public void doPost(HttpServletRequest req,HttpServletResponse res) throws
- ServletException, IOException
- {
- PrintWriter pw = res.getWriter();
- Part f=req.getPart("t1");
- try
- {
- FileOutputStream os=new FileOutputStream(new File("d:/abc.txt"));
- FileInputStream is=(FileInputStream)f.getInputStream();
- int r;
- while ((r = is.read()) != -1) {
- os.write((char)r);
- }
- pw.println("file uploaded sucessfully...!!!");
- } catch(Exception e)
- {
- pw.print(e);
- }
- }
- }
- Download file example
- Input.html
- <body>
- <form action="Download" method="get" enctype="multipart/form-data">
- <input type="submit" value="ok">
- </form>
- </body>
- Download.java
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class Download extends HttpServlet
- {
- public void doGet(HttpServletRequest req, HttpServletResponse res)throws
- ServletException, IOException
- {
- String filename="d:/abc.txt";
- FileInputStream is = new FileInputStream(filename);
- PrintWriter out=res.getWriter();
- res.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
- int i;
- while ((i=is.read()) != -1) {
- out.write(i);
- }is.close();
- out.close();
- }
- }
- 10. Create an html page with fields, eno, name, age, desg, salary. Now on submit this data to a JSP page which will update the employee table of database with matching eno.
- On Mysql
- create database db2;
- use db2;
- create table emp(eno int,name varchar(20),age int,designation varchar(20),salary int);
- insert into emp values(1,'abc',35,'manager',30000);
- insert into emp values(2,'xyz',28,'TL',23000);
- insert into emp values(3,'pqr',40,'director',60000);
- select * from emp;
- input.html
- <body>
- <form action="update.jsp">
- Emp no<input type=text name=t1>
- Name<input type=text name=t2>
- Age<input type=text name=t3>
- Designation<input type=text name=t4>
- Salary<input type=text name=t5>
- <input type=submit value=ok>
- </form>
- </body>
- update.jsp
- <body>
- <%@ page import="java.sql.*"%>
- <%! int no,age,salary;String name,designation;%>
- <%
- no=Integer.parseInt(request.getParameter("t1");
- name=request.getParameter("t2");
- age=Integer.parseInt(request.getParameter("t3");
- designation=request.getParameter("t4");
- salary=Integer.parseInt(request.getParameter("t5");
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con=DriverManager.getConnection("jdbc:mysql://localhost/db2","root","abc");
- PreparedStatement pst=con.prepareStatement("update emp set
- name=?,age=?,designation=?,salary=? where eno=?");
- pst.setString(1,name);
- pst.setInt(2,age);
- pst.setString(3,designation);
- pst.setInt(4,salary);
- pst.setInt(5,no);
- pst.execute();
- con.close();
- out.println("Data updated ");
- }
- catch(Exception e)
- {
- out.println(e);
- }
- %>
- </body>
- 11. Develop servlet application to demonstrate basic calculator. D
- Input.html
- <body>
- <form action="Cal" method="get" name="frm">
- Enter Number 1:
- <input name="numl" type="text"/>
- Enter Number 2:
- <input name="num2" type="text" />
- Operator
- <select name="operator"/>
- <option value="Addition">+</option>
- <option value="Subtraction">-</option>
- <option value="Multiplication">*</option>
- <option value="division">/</option>
- </select>
- <input type="submit" value="submit"/>
- </form></body>
- Cal.java
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class Cal extends HttpServlet
- {
- public void doGet(HttpServletRequest req,
- HttpServletResponse res) throws ServletException,IOException
- {
- PrintWriter out = res.getWriter();
- String num1 = req.getParameter("numl");
- String num2 = req.getParameter("num2");
- String op = req.getParameter("operator");
- if(op.equals("Addition")){
- out.println("Addition is :"+(Integer.parseInt(num1) + Integer.parseInt(num2)));
- }
- else if(op.equals("Subtraction"))
- {
- out.println("Subtraction is:"+ (Integer.parseInt(num1)-Integer.parseInt(num2)));
- }
- else if(op.equals("Multiplication")){
- out.println("Multiplication is:"+(Integer.parseInt(num1)*Integer.parseInt(num2)));
- }
- else{ out.println("Divisionis:"+(Integer.parseInt(num1)/Integer.parseInt(num2))); }}}
- 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. D
- Input.html
- <body>
- <form name=f1 method=get action="Login" >
- Enter UserId <input type="text" name="t1" ><br>
- Enter Password<input type="text" name="t2" ><br>
- <input type="submit" value="OK" >
- </form>
- </body>
- Login.java
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class Login extends HttpServlet
- {
- public void doGet(HttpServletRequest req,
- HttpServletResponse res) throws ServletException,IOException
- {
- PrintWriter pw=res.getWriter();
- String u= req.getParameter("t1");
- String p= req.getParameter("t2");
- if(u.equals("tyit") && p.equals("java"))
- {
- RequestDispatcher
- rd1=req.getRequestDispatcher("Result");
- rd1.forward(req,res);
- } else
- {
- RequestDispatcher
- rd2=req.getRequestDispatcher("input.html”);
- pw.println("Invalid user.Please try again");
- rd2.include(req,res);
- }
- }
- }
- Result.java
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class Result extends HttpServlet
- {
- public void doGet(HttpServletRequest req,
- HttpServletResponse res) throws ServletException,IOException
- {
- PrintWriter pw=res.getWriter();
- pw.println("Welcome");
- }
- }
- 12. Create a JSP page to demonstrate the use of Expression language.
- 13. Develop a web application to allow the user to enter two numbers and select an operation
- [ +,-,*,/ ] in an HTML file. When the user clicks “Calculate” button, call a servlet to compute the answer and display it to the user. D
- index.html
- <body>
- <form action="Calculator" >
- Enter First Number <input type="text" name="t1" ><br>
- Enter Second Number <input type="text" name="t2" ><br>
- Select an Operation
- <input type="radio" name="r1" value="+">ADDTION
- <input type="radio" name="r1" value="-">SUBSTRACTION
- <input type="radio" name="r1" value="*">MULTIPLY
- <input type="radio" name="r1" value="/">DIVIDE <br>
- <input type="reset">
- <input type="submit" value="Calculate" >
- </form>
- </body>
- CalculatorServlet.java
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class Calculator extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException
- {
- PrintWriter out = response.getWriter();
- out.println("Servlet Calculator Servlet");
- double n1 = Double.parseDouble(request.getParameter("t1"));
- double n2 = Double.parseDouble(request.getParameter("t2"));
- double result =0;
- String r1=request.getParameter("r1");
- if(r1.equals("+")) result=n1+n2;
- if(r1.equals("-")) result=n1-n2;
- if(r1.equals("*")) result=n1*n2;
- if(r1.equals("/")) result=n1/n2;
- out.println("<h1> Result = "+result);
- } }
- 14. 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). D
- input.html
- <body>
- <form action="validate.jsp">
- Enter Your Name<input type="text" name="name" ><br>
- Enter Your Age<input type="text" name="age" ><br>
- Enter Email<input type="text" name="email" ><br>
- Select Hobbies
- <input type="checkbox" name="hob" value="Singing">Singing
- <input type="checkbox" name="hob" value="Reading">Reading Books
- <input type="checkbox" name="hob" value="Football">Playing Football<br>
- Select Gender
- <input type="radio" name="gender" value="male">Male
- <input type="radio" name="gender" value="female">Female
- <input type="radio" name="gender" value="other">Other<br>
- <input type="submit" value="Submit Form">
- </form>
- </body>
- validate.jsp
- <body>
- <jsp:useBean id="obj" class="p1.Details" />
- <jsp:setProperty name="obj" property="*"/>
- <%
- if (obj.validate())
- {
- %>
- <jsp:forward page="s.jsp">
- <jsp:param name="p1" value="<%= obj.getName() %>" />
- <jsp:param name="p2" value="<%= obj.getAge() %>" />
- <jsp:param name="p3" value="<%= obj.getEmail() %>" />
- <jsp:param name="p4" value="<%= obj.getHob() %>" />
- <jsp:param name="p5" value="<%= obj.getGender() %>" />
- </jsp:forward>
- <%
- }
- else
- {
- %>
- <jsp:include page="input.html"/>
- <%= obj.getError() %>
- <%
- }
- %>
- </body>
- </body>
- Details.java
- package p1;
- public class Details
- {
- String name="", age="",hob="", email="", gender="", 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.equals(""))
- {
- error=error+"<br>Enter First Name";
- res=false;
- } if(age.equals("") )
- {
- error=error+"<br>Age Invalid";
- res=false;
- }
- return res;
- }
- }
- s.jsp
- <body>
- Welcome, <%=request.getParameter("p1") %> <br>
- <%=request.getParameter("p2") %> <br>
- <%=request.getParameter("p3") %><br>
- <%=request.getParameter("p4") %><br>
- <%=request.getParameter("p5") %>
- </body>
- 15. Write a JSP to accept eno, ename and salary. Insert these records in emp. (Create the emp table with these three fields.
- Index.jsp
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP Registration</title>
- </head>
- <body>
- <h1>Registration Form</h1>
- <form action="val.jsp" method="post">
- <table style="width:50%">
- <tr>
- <td>Full Name</td>
- <td><input type="text" name="fullname"/></td>
- <td>age</td>
- <td><input type="text" name="age"></td>
- <td>E-Mail</td><td><input type="text" name="email" size="20">
- </td></tr>
- <tr> <td>Gender</td>
- <td><input type="radio" name="gender" value="Male">Male
- <input type="radio" name="gender" value="Female"> Female
- </td> </tr>
- <tr><td>Hobbies</td>
- <td>
- <input type="checkbox" name="hb" value="Acting">Acting
- <input type="checkbox" name="hb" value="Dancing"/>Dancing
- <input type="checkbox" name="hb" value="Singing"/>Singing
- </td> </tr>
- </table>
- <input type="submit" value="Register">
- </form>
- </body>
- </html>
- val.jsp
- <%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
- <%!
- int ageInNumbers;
- private static final String EMAIL_REGEX = "^[\\w-\\+]+(\\.[\\w]+)*@[\\w]+(\\.[\\w]+)*(\\.[a-z]{2,})$";
- %>
- <%
- String name = request.getParameter("fullname");
- String age = request.getParameter("age");
- String email = request.getParameter("email");
- String gender = request.getParameter("gender");
- String hb[]= request.getParameterValues("hb");
- if(name.isEmpty()|| age.isEmpty()|| email.isEmpty()|| gender.isEmpty())
- out.println("<font color=red>Please fill all the fields</font><br>");
- if(!email.matches(EMAIL_REGEX)){
- out.println("<font color=red>Correct Your Email Address</font><br>");
- }
- try{
- ageInNumbers = Integer.parseInt(age.trim());
- }
- catch(NumberFormatException e){
- out.println("<font color=red>Age must be in numbers</font><br>");
- }
- if(ageInNumbers >= 18 && ageInNumbers >=60)
- {
- out.println("<font color=red>Age must be between 18 to 60</font><br>");
- }
- %>
- Your Entered Information is as follows: <br> <br>
- Full Name <b>:<%=name%> </b><br>
- Age <b>:<%=age%> </b><br>
- EMail <b>:<%=email%> </b><br>
- Gender <b>:<%= gender%> </b><br>
- Hobbies <b>:
- <%
- if (hb != null && hb.length !=0){
- for (int i = 0; i <hb.length; i++){
- out.println(hb[i]);
- }
- }
- %>
- web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
- version="3.1">
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <session-config>
- <session-timeout>
- 30
- </session-timeout>
- </session-config>
- </web-app>
- 16. Write a JSP to accept eno, ename and salary. Insert these records in emp. (Create the emp table with these three fields.
- Index.jsp
- <html>
- <body>
- <form action="Register" method="post">
- Eno.<input type="text" name="txt1">
- Ename.<input type="text" name="txt2">
- salary.<input type="text" name="txt3">
- <input type="submit" value="Submit" name="b1">
- </form>
- </body>
- </html>
- Register.java
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.sql.*;
- public class Register extends HttpServlet {
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- String n = request.getParameter("txt1");
- String o = request.getParameter("txt2");
- String p = request.getParameter("txt3");
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/logindb", "root", "root");
- PreparedStatement ps = con.prepareStatement("insert into emptable values(?,?,?)");
- ps.setString(1, n);
- ps.setString(2, o);
- ps.setString(3, p);
- int i = ps.executeUpdate();
- if (i > 0);
- out.print("You are successfully registered...");
- } catch (Exception e2) {
- System.out.println(e2);
- }
- out.close();
- }
- }
Add Comment
Please, Sign In to add comment