接着 我的第一个Servlet程序,为了测试一下浏览器到底是get还是post方法接收信息,修改代码如下:
package com.zzk.cn; 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; public class HelloWorldServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("dopost..."); //super.doPost(req, resp); } /** * */ private static final long serialVersionUID = 1L;//父类或者父类的父类实现了序列化的接口 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("doget...."); PrintWriter out=resp.getWriter(); out.println("<html><head></head><body><center><font color='red'>HelloWorldServlet!</font></center></body></html>"); out.flush();//清理 out.close();//关闭 } }
写一段html代码:
<form action=http://localhost:8080/my/HelloWorldServlet method=get> <input type=text name=test> <input type=submit value="提交"> </form>
<html><head></head><body><center><font color='red'>HelloWorldServlet!</font></center></body></html>的HTML页面
同时,tomcat的DOS窗口会显示doget...一次
但是此时,将html代码替换为dopost方法,如下:
<form action=http://localhost:8080/my/HelloWorldServlet method=post> <input type=text name=test> <input type=submit value="提交"> </form>
我们再来修改servlet代码:
package com.zzk.cn; 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; public class HelloWorldServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("dopost..."); //super.doPost(req, resp); doGet(req,resp); } /** * */ private static final long serialVersionUID = 1L;//父类或者父类的父类实现了序列化的接口 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("doget...."); PrintWriter out=resp.getWriter(); out.println("<html><head></head><body><center><font color='red'>HelloWorldServlet!</font></center></body></html>"); out.flush();//清理 out.close();//关闭 } }
<html><head></head><body><center><font color='red'>HelloWorldServlet!</font></center></body></html>
tomcat里显示
dopost...
doget....
接下来,重写servlet
package com.zzk.cn; 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; public class HelloWorldServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub //super.service(req, resp); //System.out.println("doget...."); PrintWriter out=resp.getWriter(); out.println("<html><head></head><body><center><font color='red'>HelloWorldServlet!</font></center></body></html>"); out.flush();//清理 out.close();//关闭 } // @Override // protected void doPost(HttpServletRequest req, HttpServletResponse resp) // throws ServletException, IOException { // // TODO Auto-generated method stub // System.out.println("dopost..."); // //super.doPost(req, resp); // doGet(req,resp); // } // // // // @Override // protected void doGet(HttpServletRequest req, HttpServletResponse resp) // throws ServletException, IOException { // // TODO Auto-generated method stub // System.out.println("doget...."); // PrintWriter out=resp.getWriter(); // out.println("<html><head></head><body><center><font color='red'>HelloWorldServlet!</font></center></body></html>"); // out.flush();//清理 // out.close();//关闭 // } /** * */ private static final long serialVersionUID = 1L;//父类或者父类的父类实现了序列化的接口 }
然后index.html中get和post方法
<form action=http://localhost:8080/my/HelloWorldServlet method=get> <input type=text name=test> <input type=submit value="提交"> </form>
<form action=http://localhost:8080/my/HelloWorldServlet method=post> <input type=text name=test> <input type=submit value="提交"> </form>