qq_32230313
qq_32230313
2015-11-03 05:45
采纳率: 100%
浏览 2.1k
已采纳

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条回答 默认 最新

  • wojiushiwo945you
    毕小宝 2015-11-03 05:56
    已采纳

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

    点赞 评论
  • u014493323
    xpbobcsdn 2015-11-03 06:00

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

    点赞 评论
  • qq_17637725
    super_Leon 2015-11-03 08:04

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

    点赞 评论
  • yugege5201314
    yuge_Bwqz 2015-11-08 16:51

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

    点赞 评论

相关推荐