servlet跳转到jsp,怎么把传入的值赋到里面的jsp表单的text中
 add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>计算器</title>
    <script type="text/javascript" src="result.js">
    </script>
</head>
<body>
<%
    String first = request.getParameter("first");
    String second = request.getParameter("second");
%>
    <form action="/add" method="get">
        <input id="first" type="text" name="first" value=first>
        <select name="fuhao">
            <option>+</option>
            <option>-</option>
            <option>*</option>
            <option>/</option>
        </select>
        <input id="second" type="text" name="second" value=second> 
        <input id="count" type="submit" value="=" onclick="add()">
        <input id="result" type="text"> 
    </form>

</body>
</html>


addServlet.java
package cn.itsource.servletadd;

import java.io.IOException;

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("/add")
public class AddServlet extends HttpServlet{
    private static final long serialVersionUID = 1L;
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
//      得到传来的第一个加数
        String first  = req.getParameter("first");
//      System.out.println(first);
        Integer firstadd =null;
//      判断输入的值是否是空值,并转换成Integer
        if(first!=null){
            firstadd = Integer.parseInt(first);
        }
//      System.out.println(firstadd);
//      得到第二个加数
        String second  = req.getParameter("second");
        Integer secondadd = null;
//      System.out.println(second);
//      判断输入的值是否是空值,并转换成Integer
        if(second!=null){
            secondadd = Integer.parseInt(second);
        }
//      得到传入的符号是什么
        String countadd  = req.getParameter("fuhao");
//      System.out.println(countadd);
//      得到最后的结果
        String result  = req.getParameter("result");
        if("+".equals(countadd)){
            Integer rel = firstadd + secondadd;
            result = rel.toString();
        }
        if("-".equals(countadd)){
            Integer rel = firstadd - secondadd;
            result = rel.toString();
        }
        if("*".equals(countadd)){
            Integer rel = firstadd * secondadd;
            result = rel.toString();
        }
        if("/".equals(countadd)){
            Integer rel = firstadd / secondadd;
            result = rel.toString();
        }
//      System.out.println(result);
        req.getRequestDispatcher("/add.jsp").forward(req, resp);

    }
}
怎么把跳转的值设置到上面的表单中的text里面

4个回答

可以用js在文档完成加载后进行表单赋值操作。

你可以把值存到request域中,在jsp页面取出,在text的value中直接赋值

就像楼上所说的放进request域中,列如:后台存放 request.setAttribute("name","zhangsan"); 前台获取:
这样你前台input框里就能获取你后台放的值

按楼上说的 ,然后JsP页面这样拿值${name}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问