You are on page 1of 10

NAME: VITHURUN V

Reg No: 20MID0229


Course: Advanced java
Slot: L53+L54

ASSESSMENT – 2

1) You are asked to carry out the registration process of an event. First, you need to
provide the form, which includes the participant personal details (Name, Id,
Designation, College). After the registration, you have to provide the confirmation like
“Successfully registered”. Then, the registered participant need to login to the event
by using his/her name and Id. You need to display that the number of times the
participant has attended the event through login.

Creating database:
Login page HTML:

Registration page HTML:

Servlet code
Login servlet:

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;
import jakarta.servlet.http.HttpSession; import
java.sql.*;

public class Servlet extends HttpServlet {


@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html"); HttpSession
session = request.getSession();
response.setContentType("text/html");
PrintWriter out=response.getWriter();
Integer attribute=(Integer)session.getAttribute("attribute");
String name = request.getParameter("Username");
try {
Class.forName("com.mysql.jdbc.Driver");
try (Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/home",
"root" , "sha19@" )) {
ResultSet
rs=con.createStatement().executeQuery("select
* from advjava where username='"+name+"' ");
if(rs.next()) { if (attribute == null)
{
attribute = 1;
out.println("<h1><center>NewUser
"+name+"</center></h1>");
} else {
out.println("<h1><center>Welcome back
"+name+"</center></h1>");
attribute = attribute+ 1;
}
} else {
out.print("Not a Registered User,Register First!!");
}
session.setAttribute("attribute",attribute);
out.println("<h2>Number of time visit:"+attribute+"</h2>");
}
}
catch (ClassNotFoundException | SQLException e) {
out.print(e);
}
}
}

Register Servlet:

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;
import java.sql.*;

public class NewServlet extends HttpServlet {


@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

String name = request.getParameter("Username");


String id = request.getParameter("ID");
String desig = request.getParameter("designation");
String colleg = request.getParameter("college"); try {
Class.forName("com.mysql.jdbc.Driver");
try (Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/home",
"root" , "sha19@" )) {
ResultSet
rs=con.createStatement().executeQuery("select
* from advjava where username='"+name+"' ");
if(rs.next()) {
out.println(" <h1>Welcome "+name+"</h1>");
}
else {
PreparedStatement ps=con.prepareStatement("INSERT
INTO advjava(username,ID,designation,college) VALUES(?,?,?,?)");
ps.setString(1,name); ps.setString(2,id);
ps.setString(3,desig); ps.setString(4,colleg);
int x=ps.executeUpdate(); if(x>0){
out.print("<h1>Registered Successfully<h1>");
} else {
out.print("<h1>Unable to register,Try
again.Later!!<h1>");
}
} }
}
catch (ClassNotFoundException | SQLException e) {
out.print(e);
}
}
}

OUTPUT OF SERVLET:

You might also like