form錶單中的method值可以用後臺的sevlet獲得嗎?

CSDN問答 2022-01-07 17:22:28 阅读数:601

form method 可以用 sevlet

我記得用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。

這是啥原因呢?我記得之前是可以這樣用的。




采納答案:

servlet的doget無法獲得method,輸出結果為null。其他數據可以獲得,也可以向數據庫中添加數據。



其他答案2:

form錶單中的method屬性是指你本次提交錶單數據的方式是以get方式還是post方式提交,method的值只能從[get,post]中選擇一個。一般提交錶單都建議以post的方式提交。

使用post方式提交則在servlet的doPost方法中接收數據,get方式提交則在doGet方法中接收。

對錶單進行下面修改: 

<form action="UserAddorLogin?method=UserAdd" method="post" class="form-horizontal" role="form"></form>

其他答案3:
request.getMethod();
版权声明:本文为[CSDN問答]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201071722276384.html