Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之12.Servlet基础(3)
版权声明:原创作品,谢绝转载!否则将追究法律责任。 |
–提交表单的方法
• get • post –Servlet 生命周期 –使用Servlet 输出HTML页面 –获得Servlet初始化参数 –页面导航 • 请求重定向 –response.sendRedirect(“url”); • 请求转发 –request.getRequestDispatcher(“url”).forward(request,response); • 请求包含 –request.getRequestDispatcher(“url”).include(request,response); ############Michael分割线################
请求重定向
ServletBasic.java
package com.michael.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletBasic extends HttpServlet { /** * Constructor of the object. */ public ServletBasic() { super(); System.out.println("-----ServletBasic-----"); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); System.out.println("-----destroy-----"); } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("username="+username); response.sendRedirect("http://redking.blog.51cto.com"); /* if(username!=null&&username.equals("redking")){ request.getRequestDispatcher("/successfull.html").forward(request,response); }else{ request.getRequestDispatcher("/failure.html").forward(request,response); } */ /* System.out.println("-----doGet-----"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); */ } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("-----doPost-----"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occure */ public void init() throws ServletException { /* //第一种方法 String driver = this.getServletContext().getInitParameter("driver"); String url = this.getServletContext().getInitParameter("url"); System.out.println(driver); System.out.println(url); //第二种方法 Enumeration enu = this.getServletContext().getInitParameterNames(); while(enu.hasMoreElements()){ String name = (String) enu.nextElement(); String value = this.getServletContext().getInitParameter(name); System.out.println(name+":"+value); } */ String username = this.getInitParameter("username"); String password = this.getInitParameter("password"); System.out.println(username); System.out.println(password); System.out.println("-----init-----"); } /* @Override protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("-----service-----"); } */ } 看下效果
重定向了
############Michael分割线################
请求包含
ServletBasic.java
package com.michael.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletBasic extends HttpServlet { /** * Constructor of the object. */ public ServletBasic() { super(); System.out.println("-----ServletBasic-----"); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); System.out.println("-----destroy-----"); } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("username="+username); //请求重定向 //response.sendRedirect("http://redking.blog.51cto.com"); //请求包含 PrintWriter out = response.getWriter(); out.println("This is Servlet Page~~~"); request.getRequestDispatcher("/successfull.html").include(request,response); /* //请求转发 if(username!=null&&username.equals("redking")){ request.getRequestDispatcher("/successfull.html").forward(request,response); }else{ request.getRequestDispatcher("/failure.html").forward(request,response); } */ /* System.out.println("-----doGet-----"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); */ } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("-----doPost-----"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occure */ public void init() throws ServletException { /* //第一种方法 String driver = this.getServletContext().getInitParameter("driver"); String url = this.getServletContext().getInitParameter("url"); System.out.println(driver); System.out.println(url); //第二种方法 Enumeration enu = this.getServletContext().getInitParameterNames(); while(enu.hasMoreElements()){ String name = (String) enu.nextElement(); String value = this.getServletContext().getInitParameter(name); System.out.println(name+":"+value); } */ String username = this.getInitParameter("username"); String password = this.getInitParameter("password"); System.out.println(username); System.out.println(password); System.out.println("-----init-----"); } /* @Override protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("-----service-----"); } */ } 测试
############Michael分割线################ 本文出自 “王乾De技术Blog[爱生活,爱学习]” 博客,谢绝转载! 本文出自 51CTO.COM技术博客 |
附件下载:
Servlet_Basic(3).rar
Servlet_Basic(3).rar








redking 
博客统计信息
热门文章
最新评论
友情链接

