JSP - 获取在同一表单/同一页面中添加结果

时间:2014-06-07 08:39:16

标签: java jsp http servlets

我试图弄清楚如何使用Java / jsp添加两个nubers并将结果放在同一页面上(在这种情况下 - 在jsp的“result”区域中。我尝试了session属性,但是有我的代码还有问题...... 我不想使用javascript等,但只有“纯java”:)

你能帮忙吗?

Servlet:

package wprowadzenie;

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;
import javax.servlet.http.HttpSession;

@WebServlet("/AddServlet.html")
public class AddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private Integer getInt(String s) {
    try {
        Integer a = new Integer(s);
        return a;
    } catch (NumberFormatException e) {
        return null;
    }
}

public void serviceRequest(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException     {

    RequestDispatcher rd = request.getRequestDispatcher("/add.jsp");
    rd.forward(request, response);

    String no1 = request.getParameter("a");
    String no2 = request.getParameter("b");
    Integer one = getInt(no1);
    Integer two = getInt(no2);

    if (one != null && two != null) {
        int result = one + two;
        System.out.println(result);
        HttpSession session = request.getSession();
        session.setAttribute("result", result);

    }

}

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException     {

    System.out.println("get");
    serviceRequest(request, response);
}

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    System.out.println("post");
    serviceRequest(request, response);
}

}

JSP:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="AddServlet.html" method="get">
    <table>
        <tr>
            <td>B</td>
            <td><input type="text" name="a" /></td>
        </tr>
        <tr>
            <td>A</td>
            <td><input type="text" name="b" /></td>
        </tr>
        <tr>
            <td>Result:</td>
            <td><input type="text" name="result" disabled="disabled"/></td>
        </tr>
    </table>
    <input type="submit" value="add"/>



</form>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

这是您修改后的代码

package wprowadzenie;

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;
import javax.servlet.http.HttpSession;

@WebServlet("/AddServlet.html")
public class AddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private Integer getInt(String s) {
    try {
        Integer a = new Integer(s);
        return a;
    } catch (NumberFormatException e) {
        return null;
    }
}

public void serviceRequest(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException     {

    RequestDispatcher rd = request.getRequestDispatcher("/add.jsp");
    rd.forward(request, response);

    String no1 = request.getParameter("a");
    String no2 = request.getParameter("b");
    Integer one = getInt(no1);
    Integer two = getInt(no2);

    if (one != null && two != null) {
        int result = one + two;
        System.out.println(result);
        request.setAttribute("result", result);

    }

}

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException     {

    System.out.println("get");
    serviceRequest(request, response);
}

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    System.out.println("post");
    serviceRequest(request, response);
}

}

你的JSP

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="AddServlet.html" method="get">
    <table>
        <tr>
            <td>B</td>
            <td><input type="text" name="a" /></td>
        </tr>
        <tr>
            <td>A</td>
            <td><input type="text" name="b" /></td>
        </tr>
        <tr>
            <td>Result:</td>
             <c:choose>
                <c:when test="${request.result ne null}">
                        <td><input type="text" name="result"  value="${request.result}" /></td>
                </c:when>
                <c:otherwise>

                </c:otherwise>
            </c:choose>

        </tr>
    </table>
    <input type="submit" value="add"/>



</form>

</body>
</html>

答案 1 :(得分:1)

<input type="text" name="result" disabled="disabled" value ='<% out.println(request.getParameter("a") + request.getParameter("a")); %>' />

提交,使用相同页面并使用上述类型的代码计算并在结果文本字段中打印结果