Здравствуйте, возник вопрос как передать данные из jsp в servlet для обработки и вернуть в ту же jsp. У меня servlet возвращает пустую страницу, подскажите, пожалуйста, где я ошибся.
Суть проекта, в том, что через форму calc.jsp мы вводим цифры и выбираем операцию, обработка запроса происходит на сервлете и он возвращает строи операций (ArrayList) обратно в сервлет где он отображается.
Servlet
public class Servlet extends javax.servlet.http.HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
process(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
process(request, response);
}
public void process(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html;charset=UTF-8");
try {
Double x = Double.valueOf(req.getParameter("x"));
Double y = Double.valueOf(req.getParameter("y"));
String operation = req.getParameter("operation");
HttpSession session = req.getSession();
String resultString = calcResult(x, y, operation);
ArrayList<String> operationsList;
if (session.isNew()) {
operationsList = new ArrayList<>();
} else {
operationsList = (ArrayList<String>) session.getAttribute("operationsList");
}
operationsList.add(resultString);
session.setAttribute("operationsList", operationsList);
session.setAttribute("sessionID",session.getId());
} catch (Exception e) {
} finally {
RequestDispatcher reqDispatcher = getServletConfig().getServletContext().getRequestDispatcher("/jsp/calc.jsp");
reqDispatcher.forward(req,res);
}
}
private String calcResult(Double x, Double y, String operation) {
double result = 0;
String oper = null;
switch (operation) {
case "addition":
result = x + y;
oper = "+";
break;
case "subtraction":
result = x - y;
oper = "-";
break;
case "multiplication":
result = x * y;
oper = "*";
break;
case "division":
result = x / y;
oper = "/";
break;
}
String res = x + " " + oper + " " + y + " = " + result;
return res;
}
}
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<form method="POST" action="servlet">
First number:<br>
<input type="text" name="x">
<br>
Second number:<br>
<input type="text" name="y">
<br>
Operation:<br>
<select name="operation">
<option value="addition">addition</option>
<option value="subtraction">subtraction</option>
<option value="multiplication">multiplication</option>
<option value="division">division</option>
</select>
<input type="submit" value="Submit">
</form>
<div>
<h3>Session ID: ${sessionID}</h3>
<p></p>
<ol>
<c:forEach items="${operationsList}" var="item">
<li>${item}</li>
</c:forEach>
</ol>
</div>
</body>
</html>
web.xml
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/jsp/servlet</url-pattern>
</servlet-mapping>