1、页面html代码:
<html>
<body>
<form ACTION="./post2" METHOD="POST">
<input name="name" type="text" id="name"/>
<input name="send" type="submit" value="send"/>
</form>
</body>
<html>
2、在servlet中定义的处理post请求的方法:
import java.io.IOException
import java.io.PrintWriter
import javax.servlet.http.*
public class post2 extends HttpServlet{
protected void doDo(HttpServletRequest request,HttpServletResponse response)
throws IOException{
获取form表单参数:
String name = request.getParameter("name")
response.setContentType("text/html")
PrintWriter out = response.getWriter()
out.println("<HTML><BODY>")
out.println("<H2>hello "+name+"</H2>")
out.println("<BR><BR>")
out.println("info:")
out.println("<BR><BR>")
out.println("<H2>metoda GET</H2>")
out.println("<BR><BR>")
out.println("SERVER_NAME="+request.getServerName()+"<BR>")
out.println("REQUEST_METHOD="+request.getMethod()+"<BR>")
out.println("QUERY_STRING="+request.getQueryString()+"<BR>")
out.println("REMOTE_HOST="+request.getRemoteHost()+"<BR>")
out.println("REMOTE_ADDR="+request.getRemoteAddr())
out.println("</BODY></HTML>")
}
@Override
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException {
doDo(request,response)
}
@Override
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException {
doDo(request,response)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)