In my project I have the following files:
- pole.jsp containing a form and a submit
- results.jsp where I display the result (it only has a title)
- A PollServlet where I set both the title in pole.jsp and results.jsp
here are the files: poll.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Poll Page</title>
</head>
<body>
<form action="/poll?action=pole" method="POST">
<div>
<a><h2><% out.print(request.getAttribute("oldTitle").toString());%>
</h2></a><br>
</div>
<br><br>
<input type="submit" name = "submit"value="submit">
</form>
</body
results.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Results</title>
</head>
<body >
<form action="/poll?action=results" method="POST">
<a><% out.print(request.getAttribute("title"));%></a>
</form>
</body>
</html>
PollServlet.java
@WebServlet(name = "PollServlet", urlPatterns = {"/poll"})
public class PollServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if (action.equals("pole")) {
request.setAttribute("oldTitle","new tile for poll.jsp ");
getServletConfig().getServletContext().getRequestDispatcher(
"/poll.jsp").forward(request, response);
} else if (action.equals("results")) {
/* set the title for results.jsp */
request.setAttribute("title","title for results.jsp");
getServletConfig().getServletContext().getRequestDispatcher(
"/results.jsp").forward(request, response);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request,
}
The problem seem to appear in the servlet's doGet()
method. I'm only able to set the value for the first item's (poll.jsp) title and not the second (results.jsp) what am I doing wrong and how can this be implemented correctly? thanks!