Index.JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Student Details Entering page</title>
</head>
<body>
<form action="StudentServlet">
Please enter the name of Student <input type="text" name="name" /><br>
Please enter the roll NO of Student <input type="text" name="rollNo" /><br>
Please enter the age of Student <input type="text" name="age" /><br>
Please enter the class of Student <input type="text" name="stream" /><br>
<input type="submit" value="submit">
</form>
</body>
</html>
Web.xml ( Deployment Descriptor)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>StudentApp</display-name>
<!-- <servlet>
<servlet-name>StudentServlet</servlet-name>
<servlet-class>com.konzern.studentapp.servlets.StudentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudentServlet</servlet-name>
<url-pattern>/StudentServlet</url-pattern>
</servlet-mapping> -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
Student Servlet
package com.konzern.studentapp.servlets;
import java.io.IOException;
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 com.konzern.studentapp.model.Student;
import com.konzern.studentapp.service.StudentService;
import com.konzern.studentapp.service.StudentServiceInf;
/**
* Servlet implementation class StudentServlet
*/
@WebServlet(description = "Student Details servlet", urlPatterns = { "/StudentServlet" })
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public StudentServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Student student = new Student();
student.setName(request.getParameter("name"));
student.setRollNo(Integer.parseInt(request.getParameter("rollNo")));
student.setAge(Integer.parseInt(request.getParameter("age")));
student.setStream(request.getParameter("stream"));
StudentServiceInf service = new StudentService();
String message = service.insert(student);
response.getWriter().append(message).append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Student Details Entering page</title>
</head>
<body>
<form action="StudentServlet">
Please enter the name of Student <input type="text" name="name" /><br>
Please enter the roll NO of Student <input type="text" name="rollNo" /><br>
Please enter the age of Student <input type="text" name="age" /><br>
Please enter the class of Student <input type="text" name="stream" /><br>
<input type="submit" value="submit">
</form>
</body>
</html>
Web.xml ( Deployment Descriptor)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>StudentApp</display-name>
<!-- <servlet>
<servlet-name>StudentServlet</servlet-name>
<servlet-class>com.konzern.studentapp.servlets.StudentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudentServlet</servlet-name>
<url-pattern>/StudentServlet</url-pattern>
</servlet-mapping> -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
Student Servlet
package com.konzern.studentapp.servlets;
import java.io.IOException;
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 com.konzern.studentapp.model.Student;
import com.konzern.studentapp.service.StudentService;
import com.konzern.studentapp.service.StudentServiceInf;
/**
* Servlet implementation class StudentServlet
*/
@WebServlet(description = "Student Details servlet", urlPatterns = { "/StudentServlet" })
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public StudentServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Student student = new Student();
student.setName(request.getParameter("name"));
student.setRollNo(Integer.parseInt(request.getParameter("rollNo")));
student.setAge(Integer.parseInt(request.getParameter("age")));
student.setStream(request.getParameter("stream"));
StudentServiceInf service = new StudentService();
String message = service.insert(student);
response.getWriter().append(message).append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Student POJO object
/**
*
*/
package com.konzern.studentapp.model;
/**
* @author Athul kannoth
*
*/
public class Student {
private String name =null;
private int rollNo = 0;
private int age = 0;
private String stream= null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getStream() {
return stream;
}
public void setStream(String stream) {
this.stream = stream;
}
}
Student Service
/**
*
*/
package com.konzern.studentapp.service;
import com.konzern.studentapp.model.Student;
/**
* @author Athul kannoth
*
*/
public interface StudentServiceInf {
public String insert(Student student);
}
/**
*
*/
package com.konzern.studentapp.service;
import java.sql.SQLException;
import com.konzern.studentapp.dao.StudentDAO;
import com.konzern.studentapp.dao.StudentDAOInf;
import com.konzern.studentapp.model.Student;
/**
* @author Gokul balan
*
*/
public class StudentService implements StudentServiceInf {
private StudentDAOInf studentDaoInf = null;
private StudentDAOInf getStudentDAO() {
studentDaoInf = new StudentDAO();
return studentDaoInf;
}
@Override
public String insert(Student student) {
try {
return getStudentDAO().insert(student);
// return "success";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Technical Failure";
}
}
Student DAO
/**
*
*/
package com.konzern.studentapp.dao;
import java.sql.SQLException;
import com.konzern.studentapp.model.Student;
/**
* @author Apple
*
*/
public interface StudentDAOInf {
public String insert(Student student) throws SQLException;
}
/**
*
*/
package com.konzern.studentapp.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.konzern.studentapp.model.Student;
/**
* @author Kouta
*
*/
public class StudentDAO implements StudentDAOInf {
private Connection getConnection() {
return MysqlConnection.connectionInstance();
}
@Override
public String insert(Student student) throws SQLException {
PreparedStatement p= getConnection().prepareStatement(" insert into ipmanagetable(ipadd,port,date) values(?,?,?)");
p.setString(1,student.getName());
p.setInt(2,student.getAge());
p.setInt(3, student.getRollNo());
p.setString(4, student.getStream());
int output = p.executeUpdate();
return "Student information saved";
}
}
MySQL Connection Class
/**
* Arjun babu
*/
package com.konzern.studentapp.dao;
import java.sql.DriverManager;
import java.sql.Connection;
/**
* @author Arjun babu
*
*/
public class MysqlConnection {
public static Connection connectionInstance() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/StudentAppDb", "username", "password");
return con;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
For Complete explanation ( Full source code explain )
No comments:
Post a Comment
Your feedback may help others !!!