2

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>
查看全部
pzh7362
pzh7362
2017/11/08 06:56
  • servlet
  • 点赞
  • 收藏
  • 回答
    私信

9个回复