在提交Form表单时,往往我们需要带一些参数传出去进行处理
下面给出关键代码,演示一下。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
</head>
<script type="text/javascript">
function send(){
var message="test form";
myForm.action = "/WebTest001/myServlet?msg=" + message;
myForm.submit();
}
</script>
<body>
<form name="myForm" action="/WebTest001/myServlet" method="post">
<input type="button" value="send" onclick="send();">
</form>
</body>
</html>这里面用到了form的name属性
另外需要注意action最前面需要加上包名,否则404,之前看到有网友不加也可以使用??
有懂得麻烦告知。
submit即开始提交form
servlet接收参数
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String msg = request.getParameter("msg").toString();
System.out.println("msg = " + msg);
}
}只需按照参数名称取即可
本文出自 “爬过山见过海” 博客,请务必保留此出处http://670176656.blog.51cto.com/4500575/1687846
原文:http://670176656.blog.51cto.com/4500575/1687846