SSSDNSY 2017-11-08 06:56 采纳率: 0%
浏览 7530
已结题

request.getParameter("");获取到的值全是null

package logreg;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RegServlet extends HttpServlet{

    private static final long serialVersionUID = -7345215856043094964L;

    @Override
    public void init() throws ServletException {
        System.out.println("初始化"+this.getClass().toString());
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
         req.setCharacterEncoding("UTF-8");
         String id =   req.getParameter("userid");
         String user = req.getParameter("username");
         String upwd = req.getParameter("userpsd");
         String sex = req.getParameter("sex");
         String[] ints = req.getParameterValues("inst");
         String dep = req.getParameter("dep");
         String note = req.getParameter("note");
         String hiddenfield = req.getParameter("hiddenfield");
//       Map<String,String> user = req.getParameterMap();
         String inteStr ="";
         for (int i = 0;ints!=null&&i < ints.length; i++) {
            if(i==ints.length-1){
                inteStr+=ints[i];
            }else{
                inteStr+=ints[i]+",";
            }
        }
         resp.setCharacterEncoding("UTF-8");
         resp.setContentType("utf-8");
         PrintWriter out = resp.getWriter();
         String htmlStr = "<html><table><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>" +
                              "<tr><td>填写的编号:</td><td>{0}</td></tr>" +
                              "<tr><td>填写的用户名:</td><td>{1}</td></tr>" +
                             "<tr><td>填写的密码:</td><td>{2}</td></tr>" +
                             "<tr><td>选中的性别:</td><td>{3}</td></tr>" +
                             "<tr><td>选中的部门:</td><td>{4}</td></tr>" +
                             "<tr><td>选中的兴趣:</td><td>{5}</td></tr>" +
                             "<tr><td>填写的说明:</td><td>{6}</td></tr>" +
                             "<tr><td>隐藏域的内容:</td><td>{7}</td></tr>" +
                            "</html></table>";
          htmlStr = MessageFormat.format(htmlStr,id,user,upwd,sex,dep,ints,note,hiddenfield);
         out.write(htmlStr);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
}

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html ">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Register</title>
</head>
<body>
<fieldset width="300px">
    <legend>注册用户信息</legend>
    <form action="${request.getContextPath()}/uploaddownload/doreg" id="userInfo" method="post" >
        编&nbsp;&nbsp;号<input type="text" name="userid" id="userid" value="NO." size="2" maxlength="2"/><br>
        用户名&nbsp;     <input type="text" name="username" id="username" value="" size="10" maxlength="10" required/><br>
        密&nbsp;&nbsp;码<input type="password" name="userpsd" value="" size="6" maxlength="6" required/><br>
        性&nbsp;&nbsp;别<input type="radio" name="sex" value="男" />男<input type="radio" name="sex" value="女" />女<br>
        部&nbsp;&nbsp;门<select name="dep">
                            <option>人事部</option>
                            <option>研发部</option>
                            <option>实施部</option>
                        </select><br>
        兴&nbsp;&nbsp;趣<input type="checkbox" name="inst" value="" >唱歌
                        <input type="checkbox" name="inst" value="" >跳舞
                        <input type="checkbox" name="inst" value="" >打游戏
                        <input type="checkbox" name="inst" value="" >编程
                        <input type="checkbox" name="inst" value="" >旅游<br>
        说&nbsp;&nbsp;明<textarea rows="10" cols="30" name="note"></textarea> 
        <input type="hidden" name="hiddenfield" value="this is a hidden input value."/>
        <input type="reset" value="重置" /><input type="submit" value="提交" />             
    </form>
</fieldset>
</body>
</html>
  <servlet>
        <servlet-name>RegServlet</servlet-name>
        <servlet-class>logreg.RegServlet</servlet-class>
    </servlet>
     <servlet-mapping>
        <servlet-name>RegServlet</servlet-name>
        <url-pattern>/doreg</url-pattern>
    </servlet-mapping>
  • 写回答

9条回答 默认 最新

  • 一缕清风007 2017-11-08 09:57
    关注

    虽然楼主应该把负责post请求的逻辑处理放在doPost方法里面,但是明显他的问题不在这里啊,他的doPost方法里不是原样转给doGet了么

    评论

报告相同问题?

悬赏问题

  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!