我记得用req.getParameter("method")可以获得method值啊。
这是我的表单form里设置了method值。
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<style>
</style>
</head>
<body>
<form action="UserAddorLogin" method="UserAdd" class="form-horizontal" role="form">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">用户名:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail3" name="username" placeholder="name">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">密码:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword3" name="password" placeholder="Password">
</div>
</div>
<div class="form-group">
<label for="reinputPassword3" class="col-sm-2 control-label">确认密码:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="reinputPassword3" name="repassword" placeholder="Password">
</div>
</div>
<div class="form-group">
<label for="phone" class="col-sm-2 control-label">手机号:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="phone" name="phone" placeholder="phone">
</div>
</div>
<div class="form-group">
<label for="emal" class="col-sm-2 control-label" >电子邮件:</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="emal" name="emal" placeholder="emal">
</div>
</div>
<div class="form-group">
<label for="address" class="col-sm-2 control-label">收件地址:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="address" name="address" placeholder="address">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-2">
<button type="submit" class="btn btn-default">注册</button>
</div>
<div class="col-sm-offset-2 col-sm-2">
<button type="reset" class="btn btn-default">重置</button>
</div>
</div>
</form>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script type="text/javascript" src="js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script type="text/javascript" src="js/bootstrap.min.js"></script>
</body>
</html>
在servlet 的doget中我用req.getParameter("method")获取值。但是输出来的数值为null。
package com.shop.servlet.user;
import com.shop.entity.User;
import com.shop.service.UserDao;
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 java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/UserAddorLogin")
public class DoUserAdd extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
System.out.println("post");
String method=req.getParameter("method");
if("UserLogin".equals(method)){
UserLogin(req,resp);
}
if("AddUser".equals(method)){
UserAdd(req,resp);
}
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
// System.out.println("get");
// String method=req.getParameter("method");
// System.out.println(method);
// if("UserLogin".equals(method)){
// UserLogin(req,resp);
// }
// if("UserAdd".equals(method)){
// UserAdd(req,resp);
// }
String method=req.getParameter("method");
System.out.println(method);
UserAdd(req,resp);
}
protected void UserLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
resp.setContentType("text/plain;charset=utf-8");
req.setCharacterEncoding("utf-8");
String username=req.getParameter("username");
String password=req.getParameter("password");
System.out.println(username+"\n"+password);
boolean ju=UserDao.selectUser(username,password);
if(ju){
resp.sendRedirect("index.html");
}else {
System.out.println("登录失败");
PrintWriter out=resp.getWriter();
out.write("<script>");
out.write("alert('登录失败')");
out.write("</script>");
}
}
protected void UserAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/plain;charset=utf-8");
req.setCharacterEncoding("utf-8");
String name=req.getParameter("username");
String pwd=req.getParameter("password");
String phone=req.getParameter("phone");
String emal=req.getParameter("emal");
String address=req.getParameter("address");
User u=new User(null,name,pwd,phone,emal,address,1);
//System.out.println(name+"\n"+pwd);
int count=UserDao.insert(u);
//成功失败重定向
if(count>0){
resp.sendRedirect("loginin.html");
}else {
System.out.println("添加失败");
PrintWriter out=resp.getWriter();
out.write("<script>");
out.write("alert('用户添加失败')");
out.write("location.href='addme.html'");
out.write("</script>");
}
}
}
表单中的其他数据是可以获得的,也可以把用户添加到数据库,唯独不可以获得method值。
查看网页的请求发现网页并没有向后台发送method。
这是啥原因呢?我记得之前是可以这样用的。