Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- <html><head><title>Login Form</title></head>
- <form action="LoginServlet" >
- Enter User ID<input type="text" name="txtId"><br>
- Enter Password<input type="password" name="txtPass"><br>
- <input type="reset"><input type="submit" value=" Click to Login " ></form></html>
- LoginServlet.java
- import java.io.*;
- import javax.servlet.ServletException;
- import javax.servlet.http.*;
- public class LoginServlet extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter();
- out.println("<html><head><title>Servlet LoginServlet</title></head>"); String uname = request.getParameter("txtId");
- String upass = request.getParameter("txtPass"); if(uname.equals("sid") && upass.equals("12345")){ out.println("<body bgcolor=blue >"); out.println("<h1> Welcome !!! "+uname+"</h1>"); }
- else{
- out.println("<body bgcolor=red >");
- out.println("<h1> Login Fail !!! </h1>");}
- out.println("</body></html>");}}
- 2.Create a JSP application to demonstrate the use of JSTL. (add jstl-1.2 and go to web-inf add –other-web.xml)
- 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>Index Page</title> </head> <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> <h1>Hello World!</h1> </body></html>
- jstlDemo.jsp
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP 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/> <br/>
- use of if statement <br> <c:set var="mycount" value="25"/>
- <b> <c:out value="your count is 25"/> </b> <br> <br>
- use of for each statment <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
- Index.jsp (glass fish server and normal web applictaion)
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <html><head><title>JSP Page</title></head><body>
- <h1>Use of Intrinsic Objects in JSP</h1>
- <h1>Request Object </h1>
- Query String <%=request.getQueryString() %><br>
- Context Path <%=request.getContextPath() %><br>
- Remote Host <%=request.getRemoteHost() %><br>
- <h1>Response Object </h1>
- Character Encoding Type <%=response.getCharacterEncoding() %><br>
- Content Type<%=response.getContentType() %><br> Locale <%=response.getLocale() %><br>
- <h1>Session Object </h1>
- ID <%=session.getId() %><br>
- Creation Time <%=new java.util.Date(session.getCreationTime()) %><br>
- Last Access Time<%=new java.util.Date(session.getLastAccessedTime()) %><br></body>
- </html>
- 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.html
- <html><head><title>Registration Page</title></head>
- <body>
- <form action="RegisterServlet" >
- <H1>Welcome to Registration page</H1>
- Enter User Name
- <input type="text" name="txtUid"><br>
- Enter Password
- <input type="password" name="txtPass"><br>
- Enter Email
- <input type="text" name="txtEmail" ><br>
- Enter Country
- <input type="text" name="txtCon" ><br>
- <input type="reset" ><input type="submit" value="REGISTER" >
- </form>
- </body> </html>
- RegisterServlet.java
- import java.io.*;
- import java.sql.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class RegisterServlet extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String id = request.getParameter("txtUid");
- String ps = request.getParameter("txtPass");
- String em = request.getParameter("txtEmail");
- String co = request.getParameter("txtCon");
- try{
- Class.forName("com.mysql.jdbc.Driver");
- Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/logindb","root","root");
- 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("<h1>"+row+ " Inserted Succesfullyyyyy"); }catch(Exception e){out.println(e);}}}
- 6. Develop simple EJB application to demonstrate Servlet Hit count using Singleton Session Beans.
- CounterBean.java (package name=counter.ejb)
- package counter.ejb;
- import javax.ejb.Singleton;
- @Singleton
- public class CounterBean {
- private int hits = 1;
- public int getHits() {
- return hits++; } }
- Count.java(package name=mypack)
- package mypack;
- import java.io.Serializable;
- import javax.ejb.EJB;
- import javax.enterprise.context.ConversationScoped;
- import javax.inject.Named;
- import counter.ejb.CounterBean;
- @Named("count")
- @ConversationScoped
- public class Count implements Serializable {
- @EJB
- private CounterBean counterBean;
- private int hitCount;
- public Count() {
- this.hitCount = 0;}
- public int getHitCount() {
- hitCount = counterBean.getHits();
- return hitCount; }
- public void setHitCount(int newHits) {
- this.hitCount = newHits; } }
- 7.Develop a JSP application to authenticate user login with database.
- 8.Create a JSP page to demonstrate the use of Expression language.
- 9.Create a Servlet application to upload and download a file.
- Index.html
- <html>
- <body>
- <form action="FileUploadServlet" method="post" enctype="multipart/form-data"> Select File to Upload:<input type="file" name="file" id="file">
- Destination <input type="text" value="/tmp" name="destination"><br>
- <input type="submit" value="Upload file" name="upload" id="upload">
- </form> </body> </html>
- FileUploadServlet.java
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.annotation.MultipartConfig;
- import javax.servlet.http.*;
- @MultipartConfig
- public class FileUploadServlet extends HttpServlet {
- public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,
- IOException
- {res.setContentType("text/html");
- PrintWriter out = res.getWriter();
- String path=req.getParameter("destination");
- Part filePart=req.getPart("file");
- String filename=filePart.getSubmittedFileName().toString();
- out.print("<br><br><hr> file name: "+filename);
- OutputStream os=null;
- InputStream is=null;
- try { os=new FileOutputStream(new File(path+File.separator+filename));
- is=filePart.getInputStream();
- int read=0;
- while ((read = is.read()) != -1) {
- os.write(read);
- } out.println("<br>file uploaded sucessfully...!!!"); }
- catch(FileNotFoundException e){out.print(e); } } }
- index.html
- <html> <head> <title>File Download Page</title> </head> <body>
- <h1>File Download Application</h1>
- Click <a href="DownloadServlet?filename=SampleChapter.pdf">Sample Chapter</a><br/><br/>
- Click <a href="DownloadServlet?filename=TOC.pdf">Table Of Contents</a></body> </html>
- DownloadServlet.java
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.PrintWriter;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class DownloadServlet extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException { response.setContentType("APPLICATION/OCTET-STREAM");
- String filename = request.getParameter("filename");
- ServletContext context = getServletContext();
- InputStream is = context.getResourceAsStream("/" + filename);
- PrintWriter out=response.getWriter();
- response.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.
- 11.Develop servlet application to demonstrate basic calculator.
- index.html
- <html><head><title>Calculator App</title></head><body>
- <formaction="CalculatorServlet" >
- Enter First Number
- <input type="text"name="txtN1"><br>
- Enter Second Number
- <input type="text" name="txtN2" ><br> Select an Operation
- <input type="radio" name="opr" value="+">ADDTION
- <input type="radio" name="opr" value="-">SUBSTRACTION <input type="radio" name="opr" value="*">MULTIPLY
- <input type="radio" name="opr" value="/">DIVIDE <br>
- <input type="reset">
- <input type="submit" value="Calculate" >
- </form></body></html>
- CalculatorServlet.java
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class CalculatorServlet extends HttpServlet {
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter(); {
- out.println("<html><head><title>Servlet CalculatorServlet</title></head><body>"); double n1 = Double.parseDouble(request.getParameter("txtN1"));
- double n2 = Double.parseDouble(request.getParameter("txtN2"));
- double result =0;
- String opr=request.getParameter("opr");
- 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("<h1> Result = "+result);
- out.println("</body></html>"); } } }
- 12.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
- <html><head><title>Login Form</title></head>
- <form action="LoginServlet" >
- Enter User ID<input type="text" name="txtId"><br>
- Enter Password<input type="password" name="txtPass"><br>
- <input type="reset"><input type="submit" value=" Click to Login " ></form></html>
- LoginServlet.java
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.RequestDispatcher;
- public class LoginServlet extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter();
- out.println("<html><head>");
- out.println("<title>Servlet LoginServlet</title></head>");
- String uname = request.getParameter("txtId");
- String upass = request.getParameter("txtPass");
- if(uname.equals("sid") && upass.equals("12345")){
- RequestDispatcher rd = request.getRequestDispatcher("WelcomeServlet");
- rd.forward(request, response); }
- else{
- out.println("<body bgcolor=red >");
- out.println("<h1> Login Fail !!! </h1>");
- RequestDispatcher rd = request.getRequestDispatcher("index.html");
- rd.include(request, response);}
- out.println("</body>");
- out.println("</html>");}}
- WelcomeServlet.java
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.RequestDispatcher;
- public class WelcomeServlet extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter();
- out.println("<html><head>");
- out.println("<title>Servlet LoginServlet</title></head>"); String uname = request.getParameter("txtId");
- String upass = request.getParameter("txtPass");
- if(uname.equals("sid") && upass.equals("servlet")){
- RequestDispatcher rd = request.getRequestDispatcher("LoginServlet");
- rd.forward(request, response);}
- else{
- out.println("<body bgcolor=blue >");
- out.println("<h1> welcome </h1>");
- RequestDispatcher rd = request.getRequestDispatcher("index.html");
- rd.include(request, response);}
- out.println("</body>");
- out.println("</html>"); } }
- 13.Create a JSP page to demonstrate the use of Expression language.
- 14.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.
- 15.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
- <html><head><title>User Information Paage</title> </head>
- <body>
- <form action="Validate.jsp">
- Enter Your Name<input type="text" name="name" ><br> Enter Your Age<input type="text" name="age" ><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>
- Enter E-mail<input type="text" name="email" ><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="hidden" name="error" value="">
- <input type="submit" value="Submit Form">
- </form> </body> </html>
- Validate.jsp
- <%@page contentType="text/html" pageEncoding="UTF-8"%><html><head><title>JSP Page</title></head><body>
- <h1>Validation Page</h1>
- <jsp:useBean id="obj" scope="request"
- class="mypack.CheckerBean">
- <jsp:setProperty name="obj" property="*"/>
- </jsp:useBean>
- <%if (obj.validate())
- { %>
- <% }
- else {%>
- <jsp:include page="index.html"/>
- <%}%>
- <%=obj.getError() %>
- </body></html>
- CheckerBean.java
- package mypack;
- 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+="<br>Enter First Name";res=false;}
- if(age.length() > 2 )
- {error+="<br>Age Invalid";res=false;}
- return res; } }
- 16.Write a JSP to accept eno, ename and salary. Insert these records in emp. (Create the emp table with these three fields.]
Add Comment
Please, Sign In to add comment