kamil.rak
kamil.rak

Reputation: 1378

JSP - Get Add result in the same form/same page

I am trying to figure out how to add two nubers and have the result on the same page using Java/jsp (in this case - in the "result" area in the jsp. I tried with session attribute, but there is still something wrong with my code... I do not want to use javascript etc, but "pure java" only :)

Could you help:

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>

Upvotes: 2

Views: 12568

Answers (2)

Vishal
Vishal

Reputation: 108

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

on submit, use same page and using above kind of code calculate and print result in result text field

Upvotes: 1

Nirav Prajapati
Nirav Prajapati

Reputation: 3005

here it is your revised code

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);
}

}

your 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>

Upvotes: 1

Related Questions