I am trying to send data from the InformationServlet servlet to the information.jsp file it redirects to. The InformationServlet is the following:
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "Information", value = "/Information")
public class InformationServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
RequestDispatcher dispatcher = request.getRequestDispatcher("administration.jsp");
dispatcher.forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String email = request.getParameter("email");
request.setAttribute("myemail",email);
request.getRequestDispatcher("information.jsp").forward(request, response); // forward the email to the .jsp
}
}
and the information.jsp is:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.sql.SQLException"%>
<%-- Database information --%>
<%
String id = request.getParameter("userid");
String driver = "com.mysql.cj.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String database = "employees";
String userid = "root";
String password = "password";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
%>
<html>
<head>
<title>information</title>
</head>
<body>
<% String member_email = (String)request.getAttribute("myemail");
try{
connection = DriverManager.getConnection(connectionUrl+database, userid, password);
statement = connection.prepareStatement("Select surname, name, email, phone, CV from member left join resume on memberEmail = email where email = ?");
statement.setString(1, member_email);
resultSet = statement.executeQuery();
resultSet.next();
%>
<div id="table">
<a action="Information" method="post" class="table-row">
<span class="table-cell" name="surname"> <%=resultSet.getString("surname") %> </span>
<span class="table-cell" name="firstname"> <%=resultSet.getString("name") %> </span>
<span class="table-cell" name="email"> <%=resultSet.getString("email") %> </span>
<span class="table-cell" name="phone"> <%=resultSet.getString("phone") %> </span>
<span class="table-cell" name="cv"> <%=resultSet.getString("cv") %> </span>
</div>
<%
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>
Basically the .jsp file retrieves some information from the database "employees" depending on the "myemail" parameter passed from the servlet. I've searched it up a lot online and from what I found this is the correct way of establishing a communication between the two file types, yet the member_email variable in the scriplet of the "information.jsp" file is null somehow. This is my first attempt of building a web application, so the usage of servlets/jsps is rather new to me, so maybe I have an obvious mistake. Also in case it is needed, the "administration.jsp" the "InformationServlet" listens from is the following:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%-- Database information --%>
<%
String id = request.getParameter("userid");
String driver = "com.mysql.cj.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String database = "employees";
String userid = "root";
String password = "password";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<html>
<head>
<title>Administration Page</title>
<link rel="stylesheet" href="css_files/initial.css">
</head>
<body>
<h1>Administration Page</h1>
<%
try{
connection = DriverManager.getConnection(connectionUrl+database, userid, password);
statement=connection.createStatement();
String sql ="select surname, name, email from member";
resultSet = statement.executeQuery(sql);
//each row will be filled with looping throw the table in the database
while(resultSet.next()){
%>
<div id="table">
<form method="post" action="information.jsp" class="table-row">
<span class="table-cell" name="surname"> <%=resultSet.getString("surname") %> </span>
<span class="table-cell" name="firstname"> <%=resultSet.getString("name") %> </span>
<span class="table-cell" name="email"> <%=resultSet.getString("email") %> </span>
<button type="submit">Navigate to user information</button>
</form>
</div>
<%
}
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>
which is working as intended.