0% found this document useful (0 votes)
4 views

Adv Java Exp-4

Uploaded by

Pinku 230
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Adv Java Exp-4

Uploaded by

Pinku 230
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Name Of The Experiment :

*Coding Phase: Pseudo Code / Flow Chart / Algorithm :


Registration of Student Data :-
1. Start
2. Initialize “RegModel” with Name, Email, Age from form input
3. Load JDBC Driver
4. Establish Database Connection
5. Prepare SQL Insert Statement
6. Set Parameters from “RegModel”
7. Execute Insert Statement
8. Insert successful :
• Yes: Print "Registration Successful!"
• No: Print "Registration Failed!"
9. End

JSP Form :-
1. User opens “index.jsp” and sees the registration form.
2. User fills in “Name”, “Email”, and “Age”.
3. User submits the form, which sends a POST request to “project”.

Servlet Handling :-
1. Servlet receives POST request.
2. Servlet retrieves form data (Name, Email, Age).
3. Servlet creates “RegModel” object with form data.
4. Servlet calls “registrationDao.saveRegistration(regModel)” to save data to the database.
5. Servlet prints the registration result to the response.

* Testing Phase: Compilation of Code (error detection):

As applicable according to the experiment.


Two sheets per experiment (10-20) to be used.
Applied and Action Learning

*Implementation Phase:Final Output (No Error)


RegModel.java :-
package com.project.model;
public class RegModel {
private String Name;
private String Email;
private int Age;

public RegModel(String Name, String Email, int Age) {


this.Name = Name;
this.Email = Email;
this.Age = Age;
}

public String getName() {


return Name;
}

public void setName(String Name) {


this.Name = Name;
}

public String getEmail() {


return Email;
}

public void setEmail(String Email) {


this.Email = Email;
}

public int getAge() {


return Age;
}

public void setAge(int age) {


this.Age = age;
}

@Override
public String toString() {
return "RegModel [name=" + Name + ", email=" + Email + ", age=" + Age + "]";
}
}

registrationDao.java :-
package com.project.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import com.project.model.RegModel;

As applicable according to the experiment.


Two sheets per experiment (10-20) to be used.
Applied and Action Learning

*Implementation Phase:Final Output (No Error)


public class registrationDao {
private static final String URL = "jdbc:mysql://localhost:3306/servlet1";
private static final String USER = "root";
private static final String PASSWORD = "Arman5656";
private static final String DRIVER_CLASS = "com.mysql.cj.jdbc.Driver";

static {
try {
Class.forName(DRIVER_CLASS);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

public static Connection getConnection() throws SQLException {


return DriverManager.getConnection(URL, USER, PASSWORD);
}

public boolean saveRegistration(RegModel regModel) {


boolean status = false;
try (Connection connection = registrationDao.getConnection()) {
String query = "INSERT INTO registration (Name, Email, Age) VALUES (?, ?, ?)";
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, regModel.getName());
ps.setString(2, regModel.getEmail());
ps.setInt(3, regModel.getAge());
status = ps.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return status;
}
}

project.java :-
import java.io.IOException;
import com.project.dao.registrationDao;
import com.project.model.RegModel;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/project")
public class project extends jakarta.servlet.http.HttpServlet {
private static final long serialVersionUID = 1L;

public project() {
super();
// TODO Auto-generated constructor stub
}

*As applicable according to the experiment.


Two sheets per experiment (10-20) to be used.
Applied and Action Learning

*Implementation Phase:Final Output (No Error)


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
// TODO Auto-generated method stub
String Name= request.getParameter("Name");
String Email= request.getParameter("Email");
int Age = Integer.parseInt(request.getParameter("Age"));
response.getWriter().append("Name: "+ Name +"\n Email:" + Email + "\n Age:" + Age +"\n");
RegModel regModel = new RegModel(Name, Email, Age);
registrationDao dao = new registrationDao();
boolean isSaved = dao.saveRegistration(regModel);

if (isSaved) {
response.getWriter().append("Registration Successful!");
} else {
response.getWriter().append("Registration Failed!");
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,


IOException {
// TODO Auto-generated method stub
doPost(request, response);
}

index.jsp :-

<html>
<body>
<h2>Registration</h2>
<form action="project" method= "post">
Name: <input type ="text" name= "Name"> <br>
Email: <input type ="email" name= "Email"> <br>
Age: <input type ="number" name= "Age"> <br>
<input type= "submit" value= "Register">
</form>
</body>
</html>

*As applicable according to the experiment.


Two sheets per experiment (10-20) to be used.
Applied and Action Learning
* Final Output (no error) :

Rubrics FULL MARK

Concept 10
Planning and Execution/ 10
Practical Simulation/ Programming
Result and Interpretation 10
Record of Applied and Action Learning 10
Viva 10
Total 50

Signature of the Faculty: Signature of the Student:

*As applicable according to the experiment.


Two sheets per experiment (10-20) to be used.

You might also like