Issue
I am learning JSP/Servet and try to make a simple Servlet controller.
This is my project stucture:
src="https://i.stack.imgur.com/lh15S.png" alt="enter image description here">
test is the homepage, user can enter his/her hobbies. Confirm page, will confirm the details to user. It has the option to go back and edit the information or submit the information to process.jsp page.
web.xml
<?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_4_0.xsd"
id="WebApp_ID" version="4.0">
<servlet>
<servlet-name>FirstController</servlet-name>
<servlet-class>ch2.servletController.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstController</servlet-name>
<url-pattern>/ch2/servletController/Controller</url-pattern>
</servlet-mapping>
</web-app>
Test.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html lang="eu">
<head>
<meta charset="UTF-8">
<title>Simple web page</title>
</head>
<body>
<p>
This is a simple HTML page that has a form in it.
<form action="ch2/servletController/Controller">
<p>
Hobby:<input type="text" name="hobby" value="${param.hobby}">
<input type="submit" name ="confirmButton" value="confirm">
</form>
</body>
</html>
Confirm.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>Confirm Page</title>
</head>
<body>
<p>
The value of the hobby that was sent to this page is <strong>${param.hobby} </strong>.
<p>
<form action="ch2/servletController/Controller">
<p>
If there is any error, please click the edit button and
to process press the Submit button <br>
<input type="hidden" name="hobby" value="${param.hobby}">
<input type="submit" name="editButton" value="Edit">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Process.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>Process Page</title>
</head>
<body>
Thank you for your information, Your hobby of <strong>${param.hobby} </strong> will
be added to our records, eventually.
</body>
</html>
Controller.java
package ch2.servletController;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Controller")
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
String address;
if(request.getParameter("submit") != null)
{
address = "Process.jsp";
}
else if ( request.getParameter("editButton") != null)
{
address = "test.jsp";
}
else
{
address = "/Confirm.jsp";
}
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward ( request, response);
}
}
However I am not able to go back to test.jsp page and Process.jsp page:
This is the error which I am getting when I click on edit button of the confirm.jsp page.
As I can notice I am not the the correct path and even servlet location is repeating. Would it be possible to explain why servlet location is repeating and how can I succesfully navigate to test.jsp via controller?
Thanks a lot for your feedback in advance.
Edit1:
Thanks areus for the feedback. After modifying action on confirm.jsp, I am getting the below error. URL is not repeating anymore but it can't navigate back.
Please find HTML code for the confirm.jsp:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Confirm Page</title>
</head>
<body>
<p>
The value of the hobby that was sent to this page is <strong>web programming </strong>.
<p>
<form action="/ch2/servletController/Controller">
<p>
If there is any error, please click the edit button and
to process press the Submit button <br>
<input type="hidden" name="hobby" value="web programming">
<input type="submit" name="Edit" value="Edit">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Solution
On the form in Confirm.jsp
you are using a relative URL: ch2/servletController/Controller
. The URL is relative to the location of the resource that served the request (the location in the address bar of the browser), http://localhost:9191/SampleProject/ch/servletController/Controller
.
You should use an absolute URL on you Confirm.jsp
, using:
<form action="${pageContext.request.contextPath}/ch2/servletController/Controller">
Answered By - areus
Answer Checked By - Clifford M. (JavaFixing Volunteer)