- 新建工程
- 新建Servlet类
- Eclipse已经帮助我们在web.xml中配置好
- 新建html文件,命名为login.html
- login.html代码如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="Test" method="get"> <input type="text" name="test1"/> <input type="submit" value="submit"/> </form> <hr/> <form action="Test" method="post"> <input type="text" name="test2"/> <input type="submit" value="submit"/> </form> </body> </html>
- Test.java的代码为:
package com; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /**
-
Servlet implementation class Test
*/
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
/**- @see HttpServlet#HttpServlet()
*/
public Test() {
super();
// TODO Auto-generated constructor stub
}
/** - @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// response.getWriter().append("Served at: ").append(request.getContextPath());
PrintWriter writer = response.getWriter();
writer.println("this is the Get method.test1="+request.getParameter("test1"));
}
/**
- @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// doGet(request, response);
PrintWriter writer = response.getWriter();
writer.println("this is the Post method.test2="+request.getParameter("test2"));
}
}
- @see HttpServlet#HttpServlet()
- 结果:
由实验可知get和post方法的不同。
点击查看更多内容
3人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦