0% found this document useful (0 votes)
44 views44 pages

Praful EJA

The document describes 4 Java servlet practical assignments: 1. A simple calculator application using servlets to accept two numbers and an operation from a form, perform the calculation, and display the result. 2. A login servlet that checks submitted username and password and displays a welcome or failed message. 3. A registration servlet that accepts user details from a form and stores them in a database using JDBC. 4. A servlet that uses request dispatcher to validate a password, forwarding to a welcome servlet on a match and displaying an error on the original page otherwise.

Uploaded by

omkar dhaktode
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)
44 views44 pages

Praful EJA

The document describes 4 Java servlet practical assignments: 1. A simple calculator application using servlets to accept two numbers and an operation from a form, perform the calculation, and display the result. 2. A login servlet that checks submitted username and password and displays a welcome or failed message. 3. A registration servlet that accepts user details from a form and stores them in a database using JDBC. 4. A servlet that uses request dispatcher to validate a password, forwarding to a welcome servlet on a match and displaying an error on the original page otherwise.

Uploaded by

omkar dhaktode
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
You are on page 1/ 44

Kirti M Doongursee Enterprise Java

Practical No.:01
Create a simple calculator application using servlet.
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 Page</title>
</head>
<body>
<form action="cal.java">
Enter First Number<input type="text" name="txtn1" ><br><br>
Enter Second Number<input type="text" name="txtn2" ><br><br>
Select an Operation <input type="radio" name="opr" value="+">
ADDITION <input type="radio" name="opr" value="-">
SUBTRACTION <input type="radio" name="opr" value="*">
MULTIPLY <input type="radio" name="opr" value="/">
DIVIDE <br><br><input type="reset">
<input type="submit" value="Calculate" >

</form>
</body>
</html>

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

cal.java-
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class cal extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
double a=Double.parseDouble(request.getParameter("txtn1"));
double b=Double.parseDouble(request.getParameter("txtn2"));
double result =0;
String opr=request.getParameter("opr");
if(opr.equals("+")){
result=a+b;
}
else if(opr.equals("-")){
result=a-b;
}
else if(opr.equals("*")){
result=a*b;
}
else if(opr.equals("/")){

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

result=a/b;
}
}
}

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

Practical No.:02
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”.

Index.html-
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="login" >
Enter User ID<input type="text" name="txtId"><br><br>
Enter Password<input type="password" name="txtPass"><br><br>
<input type="reset"><input type="submit" value=" Click to Login " >
</form>

</body>
</html>

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

login.java-
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class login extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String un=request.getParameter("txtId");
String pass=request.getParameter("txtPass");
if(un.equals("user")&&pass.equals("123")){
out.println("Welcome"+" " +un);
}
else{
out.println("Login Failed");
}
}
}

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

Practical No.:03
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 in details.

Index.html-
<!DOCTYPE html>
<html>
<head>
<title> supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="login.java">
Enter your name<input type="text" name="txt1"><br>
Enter your surname<input type="text" name="txt2"><br>
<input type="submit"><br>
</form>
</body>
</html>

Login.java-
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class NewServlet extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet NewServlet</title>");
out.println("</head>");
out.println("<body>");
Class.forName("com.mysql.cj.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","kunal7039sutar
");
java.sql.PreparedStatement pst=con.prepareStatement("insert into kunal values(?,?)");
pst.setString(1, request.getParameter("txt1"));
pst.setString(2, request.getParameter("txt2"));
int row=pst.executeUpdate();
out.println(row+"inserted");
out.println("<h1>Servlet NewServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}catch(Exception e) {out.print(e);}

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

}
}

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

Practical No.:04
Implement the following Servlet applications with Cookies and
Sessions.
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.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 Page</title>
</head>
<body>
<form action="login" method="get">
Enter Username: <input type="text" name="un"><br>
Enter Password: <input type="text" name="pass"><br>
<input type="reset"><!-- comment -->
<input type="submit" value="Login">
</form>
</body>
</html>

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

Login.java-

import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class login extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse


response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String un=request.getParameter("un");
String pass=request.getParameter("pass");
if(un.equals("Praful")&&pass.equals("1304")){
RequestDispatcher rd=request.getRequestDispatcher("welcome");
rd.forward(request, response);
}
else{

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

out.println("login failed");
RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
rd.include(request, response);
}
}catch(Exception e){
out.println(e);
}
}

Welcome.java-
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class welcome extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse


response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

String un=request.getParameter("un");
String pass=request.getParameter("pass");
out.println("<h1>Welcome User</h1>"+un);
}catch(Exception e){
out.println(e);
}
}

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

Practical No.:05
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.

Index.html-
<html>

<body>
<form action="Page1">
Enter your name<input type="text" name="txtname"><br>
<input type="submit" value="Click to enter">
</form>
</body>
</html>

Page1.java-
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.out;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

public class Page1 extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try ( PrintWriter out = response.getWriter()) {

out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Page1</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Page1 at " + request.getContextPath() + "</h1>");
out.println(" <body bgcolor=pink>");
String uname=request.getParameter("txtname");
out.println("<h1>Welcome"+uname+"</h1>");
Cookie ck1=new Cookie("usernmame", uname);
Cookie ck2=new Cookie("visit", "1");
response.addCookie(ck1);
response.addCookie(ck2);
out.println("<h1><a href=Page2>Click to visit Page2</a></h1>");
out.println("</body>");
out.println("</html>");
}finally
{
out.close();

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

}
}

Page2.java-
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Page2 extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {

out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Page2</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Page2 at " + request.getContextPath() + "</h1>");

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

out.println("<body bgcolor=yellow>");
Cookie[] ck=request.getCookies();
for(int i=0; i<ck.length; i++)
{
if (ck[i].getName().equals("visit"))
{
int count=Integer.parseInt(ck[i].getValue())+1;
out.println("<h1>Visit No: "+count+"</h1>");
ck[i]=new Cookie("visit",count+"");
response.addCookie(ck[i]);
}
else
{
out.println("<br>");
out.println(ck[i].getName()+"="+ck[i].getValue());
}
}
out.println("<h1><a href=Page3>click to visit Page3</a></h1>");
out.println("<h1><a href=Page4>click to visit Page4</a></h1>");
out.println("</body>");
out.println("</html>");
}finally
{
out.close();
}
}

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

Page3.java-
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Page2 extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {

out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Page2</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Page2 at " + request.getContextPath() + "</h1>");
out.println("<body bgcolor=yellow>");

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

Cookie[] ck=request.getCookies();
for(int i=0; i<ck.length; i++)
{
if (ck[i].getName().equals("visit"))
{
int count=Integer.parseInt(ck[i].getValue())+1;
out.println("<h1>Visit No: "+count+"</h1>");
ck[i]=new Cookie("visit",count+"");
response.addCookie(ck[i]);
}
else
{
out.println("<br>");
out.println(ck[i].getName()+"="+ck[i].getValue());
}
}
out.println("<h1><a href=Page3>click to visit Page3</a></h1>");
out.println("<h1><a href=Page4>click to visit Page4</a></h1>");
out.println("</body>");
out.println("</html>");
}finally
{
out.close();
}
}

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

Practical No.:06
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.
Index.html-
<html>
<head><title>Session Demo</title></head>
<form action="Page1" method="get" >
Enter User ID <input type="text" name="txtName"><br>
<input type="reset" ><input type="submit" >
</form>
</html>

Page1.java-
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.out;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(urlPatterns = {"/Page1"})
public class Page1 extends HttpServlet {

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try ( PrintWriter out = response.getWriter()) {
out.println("<h1>Servlet Page1 at " + request.getContextPath() + "</h1>");
HttpSession hs=request.getSession();
if(hs.isNew())
{
out.println("<body bgcolor=yellow>");
String name=request.getParameter("txtName");
hs.setAttribute("visit","1");
out.println("<h1>Welcome Frist Time</h1>");
}
else
{
out.println("<h1>Welcome Again</h1>");
int visit= Integer.parseInt((String)hs.getAttribute("visit"))+1;
out.println("<h1>You Visited"+visit+"Time</h1>");
hs.setAttribute("visit",+visit);
}
out.println("<h1> Your Session ID"+hs.getId()+"</h>");
out.println("<h1>You logged in at "+ new
java.util.Date(hs.getCreationTime())+"</h1>");
out.println("<h1><a href=Page2>Click for Page 2</a></h1>");
out.println("<h1><a href=Page3>Click for Page 3</a></h1>");
out.println("<h1><a href=Page4>Click for Page 4</a></h1>");
out.println("<h1><a href=LogoutServlet>Click to Terminate Session</a></h1>");
out.println("</body>");

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

out.println("</html>");
}
catch(Exception e)
{
out.close();
}
}

Page2.java-
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.out;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(urlPatterns = {"/Page2"})
public class Page2 extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try ( PrintWriter out = response.getWriter()) {

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

out.println("<h1>Servlet Page2 at " + request.getContextPath() + "</h1>");


out.println("<h1>Welcome Again</h1>");
HttpSession hs = request.getSession(false);
int visit= Integer.parseInt((String)hs.getAttribute("visit"))+1;
out.println("<h1>You Visited"+visit+"Time</h1>");
hs.setAttribute("visit",+visit);
out.println("<h1> Your Session ID"+hs.getId()+"</h>");
out.println("<h1>You logged in at "+ new
java.util.Date(hs.getCreationTime())+"</h1>");
out.println("<h1><a href=Page1>Click for Page 1</a></h1>");
out.println("<h1><a href=Page3>Click for Page 3</a></h1>");
out.println("<h1><a href=Page4>Click for Page 4</a></h1>");
out.println("<h1><a href=LogoutServlet>Click to Terminate Session</a></h1>");
out.println("</body>");
out.println("</html>");
}
catch(Exception e)
{
out.close();
}
}

Page3.java-
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.out;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(urlPatterns = {"/Page3"})
public class Page3 extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try ( PrintWriter out = response.getWriter()) {

out.println("<h1>Servlet Page3 at " + request.getContextPath() + "</h1>");


HttpSession hs = request.getSession(false);
int visit= Integer.parseInt((String)hs.getAttribute("visit"))+1;
out.println("<h1>You Visited"+visit+"Time</h1>");
hs.setAttribute("visit",+visit);
out.println("<h1> Your Session ID"+hs.getId()+"</h>");
out.println("<h1>You logged in at "+ new
java.util.Date(hs.getCreationTime())+"</h1>");
out.println("<h1><a href=Page1>Click for Page 1</a></h1>");
out.println("<h1><a href=Page2>Click for Page 2</a></h1>");
out.println("<h1><a href=Page4>Click for Page 4</a></h1>");
out.println("<h1><a href=LogoutServlet>Click to Terminate Session</a></h1>");
out.println("</body>");
out.println("</html>");

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

out.println("</body>");
out.println("</html>");
}
catch(Exception e)
{
out.close();
}
}

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

Practical No.:07
Develop a simple JSP application to display values obtained
from the use of intrinsic objects of various types.
newjsp.jsp-
<%--
Document : newjsp
Created on : Sep 13, 2023, 5:49:00 PM
Author : admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<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>

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

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())%>
</body>
</html>

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

Practical No.:08
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.hmtl-
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</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
<input type="checkbox" name="hob" value="Football">Football<br>
Enter Email<input type="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">

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

</form>
</body>
</html>

Chekerbean.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)
{
name=a;
}
public void setHob(String h)
{
name=h;
}
public void setEmail(String e)
{
name=e;
}

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

public void setGender(String g)


{
name=g;
}
public void setError(String e)
{
name=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;
}

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

public String getError()


{
return error;
}
public boolean validate()
{
boolean res=true;
if(name==null)
{
error+="<br>Enter First Name";
res=false;
}
if(age==null||age.length()>2)
{
error+="<br>Age Invalid";
res=false;
}
return res;
}
}

Validate.jsp-
<%@page contentType="text/html" pageEncoding="UTF-8" import="mypack.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

<body>
<h1>Validation Page</h1>
<jsp:useBean id="obj" scope="request" class="mypack.CheckerBean">
<jsp:setProperty name="obj" property="*"/>
</jsp:useBean>

<%if(obj.validate())
{%>
<jsp:forward page="successful.jsp"/>
<%}
else {%>
<jsp:include page="index.html"/>
<%}%>
<%=obj.getError()%>
</body>
</html>

Successful.jsp-
<%--
Document : successful
Created on : Sep 13, 2023, 7:08:10 PM
Author : admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

</head>
<body bgcolor="yellow">
<% String name=request.getParameter("name");
out.print("hello "+ name);
%>
</body>
</html>

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

Practical No.:09
Create a registration and login JSP application to register and
authenticate the user based on username and password using
JDBC.

Index.html-
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="newjsp1.jsp">
Enter Username<input type="text" name="t1"><br>
Enter password<input type="password" name="t2"><br>
<input type="submit" value="Click to Login"><br>
<a href="Reg.html">Click here to new registration</a>
</form>
</body>
</html>

Register.html-
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="newjsp.jsp">
Enter name<input type="text" name="t1"><br>
Enter Email<input type="text" name="t2"><br>
Enter Password<input type="password" name="t3"><br>
Enter City<input type="text" name="t4"><br>
<input type="submit" value="Register">
</form>
</body>
</html>

Newjsp.jsp-
<%--
Document : newjsp
Created on : Sep 24, 2023, 10:03:30 PM
Author : admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

<%
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","kunal7039sutar
");
PreparedStatement pst=con.prepareStatement("insert into NewReg values (?,?,?,?)");
pst.setString(1, request.getParameter("t1"));
pst.setString(2, request.getParameter("t2"));
pst.setString(3, request.getParameter("t3"));
pst.setString(4, request.getParameter("t4"));
int row=pst.executeUpdate();
response.sendRedirect("index.html");
}catch(Exception e){out.print(e);}
%>
</body>
</html>

Newjsp1.jsp-
<%--
Document : newjsp1
Created on : Sep 25, 2023, 2:43:10 PM
Author : admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">


<title>JSP Page</title>
</head>
<body>
<%
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","kunal7039sutar
");
PreparedStatement pst=con.prepareStatement("select * from NewReg where name=? and
password=?");
pst.setString(1, request.getParameter("t1"));
pst.setString(2, request.getParameter("t2"));
ResultSet rs=pst.executeQuery();
if(rs.next())
out.print("Successully Loged in");
else
out.print("Unsuccessful Login");
}catch(Exception e){out.print(e);}
%>
</body>
</html>

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

Practical No.: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.

Sql query-

Index.html-
<html>

<body>

<form action="UpdateEmp.jsp" >

Enter Employee Number<input type="text"

name="txtEno" ><br> Enter Name<input

type="text" name="txtName" ><br>

Enter age<input type="text"

name="txtAge" ><br> Enter

Salary<input type="text" name="txtSal"

><br>

<input type="reset" ><input type="submit">

</form>

</body>

</html>

Update.jsp-
<%@page contentType="text/html" import="java.sql.*" %>

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

<html><body>

<h1>Employee Record Update</h1>

<%

String

eno=request.getParameter("txtEno

"); String

name=request.getParameter("txtNa

me"); String age =

request.getParameter("txtAge");

String sal =

request.getParameter("txtSal");

try{

Class.forName("com.mysql.jd

bc.Driver");

Connection con =

DriverManager.getConnection("jdbc:mysql://localhost:3306/emdb");

PreparedStatement stmt = con.prepareStatement("select * from emp

where empid=?"); stmt.setString(1, eno);

ResultSet rs =

stmt.executeQuery();

if(rs.next()){

out.println("<h1>~~~ Employee "+name+" Exist ~~~ </h1>");

PreparedStatement pst1= con.prepareStatement("update emp set salary=?

where empid=?"); PreparedStatement pst2= con.prepareStatement("update

emp set age=? where empid=?"); pst1.setString(1, sal); pst1.setString(2, eno);

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

pst2.setString(1, age);

pst2.setString(2, eno);

pst1.executeUpdate();

pst2.executeUpdate();

else{

out.println("<h1>Employee Record not exist !!!!!</h1>");

}catch(Exception e){out.println(e);}

%></body></html>

Praful Dhaktode Roll No.08


Kirti M Doongursee Enterprise Java

Praful Dhaktode Roll No.08

You might also like