Тема: Як передати параметри JSP -> Servlet -> JSP ?
Доброго вечора. Я новачок у джаві. І вивчаю сервлети, і зіштовхнувся з проблемою: на сторінці index.jsp вводжу дані. Post метод обробляє в MyServlet і на сторінку answerServlet.jsp видає ці значення. на ці й сторінці є ще поля вводу нових даних. коли я їх там вводжу, то після натискання на кнопку метод POST у класі AnswerServlet мені має загрузитися сторінка AnswerTwo.jsp з даними. Проте вибиває помилка Http status 404
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Вчому проблема підкажіть будь ласка. Що я зробив не так?
Tomcat 7
IntellijIdea 14
JDK 1.8
index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<form method="POST" action="/my">
<table>
<tr>
<td>Name </td>
<td><input type="text" name="firstName"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lastName"></td>
</tr>
<tr>
<td align="right" colspan="2"> <input type="submit" value="Відправити"></td>
</tr>
</table>
</form>
</body>
</html>
answerServlet.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title></title>
</head>
<body>
<h3> Answer </h3>
${textA}
${textB}
<form method="POST" action="/answer" >
<table>
<tr>
<td>Age </td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>Phone</td>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<td align="right" colspan="2"> <input type="submit" value="Відправити"></td>
</tr>
</table>
</form>
</body>
</html>
answerTwo.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title></title>
</head>
<body>
<h3>AnswerTwo</h3>
${age}
${phone}
</body>
</html>
class MyServlet:
package servlets;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class MyServlet extends HttpServlet {
private static Clas clas = new Clas();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
clas.setName(request.getParameter("firstName"));
clas.setL_name(request.getParameter("lastName"));
request.setAttribute("textA", clas.getName() );
request.setAttribute("textB", clas.getL_name() );
request.getRequestDispatcher("/answerServlet.jsp").forward(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
class AnswerServlet :
package servlets;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class AnswerServlet extends HttpServlet {
private static Clas c = new Clas();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
c.setAge(Integer.parseInt(request.getParameter("age")));
c.setPhone(request.getParameter("phone"));
request.setAttribute("age", String.valueOf(c.getName()) + " age");
request.setAttribute("phone", c.getPhone() + " phone");
request.getRequestDispatcher("/answerTwo.jpg").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/answerServlet.jsp").forward(request, response);
}
}
class Clas:
package servlets;
public class Clas {
private String name;
private String l_name;
private int age;
private String phone;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setName(String name) {
System.out.println("set name " + name );
this.name = name;
}
public void setL_name(String l_name) {
System.out.println("set l_name " + l_name );
this.l_name = l_name;
}
public String getName() {
System.out.println("get name " + name );
return name;
}
public String getL_name() {
System.out.println("get l_name " + l_name );
return l_name;
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<!--<display-name>Controller</display-name>-->
<servlet-name>myServlet</servlet-name>
<servlet-class>servlets.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/my</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>answerServlet</servlet-name>
<servlet-class>servlets.AnswerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>answerServlet</servlet-name>
<url-pattern>/answer</url-pattern>
</servlet-mapping>
</web-app>