DbConnection.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class DbConnection {
private Connection conn=null;
private PreparedStatement pstmt = null;
private String url="jdbc:mysql://localhost:3306/jsp_java?serverTimezone=UTC";
public void setConnection(){
try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn=DriverManager.getConnection(url, "root", "");
System.out.println("Connected to the database");
}catch(Exception e){
}
}
public void insertInto(nameBean nameb) {
setConnection();
try{
String query = "INSERT INTO name_1(name)VALUES(?)";
pstmt= conn.prepareStatement(query);
pstmt.setString(1, nameb.getName());
}
catch(Exception ex) {
System.out.println(ex);
}
}
}
nameBean.java
public class nameBean {
String name;
public nameBean(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ControllerServlet.java
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 javax.servlet.RequestDispatcher;
/**
* Servlet implementation class ControllerServlet
*/
@WebServlet("/")
public class ControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private DbConnection dbconn = new DbConnection();
public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path= request.getServletPath();
switch(path) {
case "/insertPath":
insertIntoFunction(request,response);
break;
default : System.out.println("error");
}
}
public void insertIntoFunction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
System.out.println("name in field:" +name);
nameBean nb = new nameBean(name);
dbconn.insertInto(nb);
RequestDispatcher rd = request.getRequestDispatcher("homejsp.jsp");
rd.forward(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
}
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>Image</title>
</head>
<body>
<form name="form1" method="post" action="<%=request.getContextPath()%>/insertPath" enctype="multipart/form-data">
Name: <input type="text" name="name"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
homepage.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>home page</title>
</head>
<body>
HI, THIS IS HOME PAGE!
</body>
</html>
Here are my files that I am using.I am directing to controller after submitting the form but I am getting null pointer exception while retriving the value of name parameter and I am unable to understand why the error is coming.