javaEE servlet实例

index.jsp

<body>
  <!-- 
   如果用表单发送post请求给Servlet,则路径上面不能带"/",原因:
     点击“提交”按钮发送的请求是客户端的请求,此时"/"参照服务器根路径,会丢失"web应用的根路径",
     解决方案:1)."/"去掉,此时servlet和页面都是参照web应用的根路径
      2).保留"/",在前面加上EL表达式${pageContext.request.contextPath },得到web应用的根路径
   -->
   <form action="${pageContext.request.contextPath }/life.action" method="post">
      <input type="submit" value="post提交">
   </form>
  </body>

HellowServlet.java

public class HelloServlet extends HttpServlet {
	// 处理get请求的方法
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	    System.out.println("处理Get请求");
	}
	// 处理post请求的方法
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("处理Post请求");
	}
}

LifeServlet.java

public class LifeServlet extends HttpServlet {

	private String str;

	public LifeServlet() {
		System.out.println("web服务器创建了Servlet实例!");
	}

	@Override
	public void init() throws ServletException {
		str = "aaa";
		System.out.println("Servlet正在初始化");
	}
  
	/**
	 * 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 {
   
		System.out.println("doGet()被调用");
	}
   //销毁
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("servlet正在销毁");
	}

	/**
	 * 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 {

		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();
	}

}

request

index.jsp

<body>
    <jsp:forward page="show.jsp"></jsp:forward>
  </body>

show.jsp

<body>
   <h2>我是show.jsp</h2>
    获取request域中的数据:${msg }
  </body>

t.jsp

<body>
    <a href="${pageContext.request.contextPath}/req.action">请求servlet</a>
   
  </body>

ForwardServlet.java

public class ForwardServlet extends HttpServlet {

	/**
	 * 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 {
		// 转发前将数据存到request域对象中
		request.setAttribute("msg", "我是request域中的数据");
		// 请求转发:在服务器内发生跳转,跳转到新的资源上(jsp页面或servlet)
		// 第1:得到请求转发器
		// RequestDispatcher dispatcher =
		// request.getRequestDispatcher("show.jsp");
		// 第2:调用forward方法,实现转发动作
		// dispatcher.forward(request, response);

		// 以上2句合写成一句
		request.getRequestDispatcher("/page/my.jsp").forward(request, response);
		
		//request.getRequestDispatcher("http://www.baidu.com").forward(request, response);
	}

	/**
	 * 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 {

		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();
	}

}

RedirectServlet.java

public class RedirectServlet extends HttpServlet {

	/**
	 * 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 {
		request.setAttribute("msg", "我是request域中的数据");
		 //重定向:跳转到新的资源上,request.getContextPath()代表获取web应用的根路径
   //  response.sendRedirect(request.getContextPath()+"/show.jsp");
     response.sendRedirect("show.jsp");
    //  response.sendRedirect("http://www.baidu.com");
	}

	/**
	 * 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 {

		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();
	}

}

RequestServlet.java

public class RequestServlet extends HttpServlet {

	/**
	 * 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 {
		//this.getRequestLine(request);
		this.getRequestHeader(request);
	}

	// 获取请求行
	public void getRequestLine(HttpServletRequest req
			) throws ServletException, IOException {
		// 获取请求方法
		String method = req.getMethod();

		// 获取请求的路径
		String url = req.getRequestURI();
		// 获取请求协议及版本号
		String protocol = req.getProtocol();
		System.out.println("请求方法:" + method);
		System.out.println("请求路径:" + url);
		System.out.println("请求协议及版本号:" + protocol);
	}

	// 获取请求头
	public void getRequestHeader(HttpServletRequest req
			) throws ServletException, IOException {
         String host=  req.getHeader("Host");
         String connection=req.getHeader("Connection");
         System.out.println(host);
         System.out.println(connection); 
	}

	/**
	 * 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 {

		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();
	}

}

ResponseServlet.java

public class ResponseServlet extends HttpServlet {

	/**
	 * 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 {
		// 添加响应头addHeader()
		response.addHeader("company", "whsxt");
		// 设置响应头setHeader(name,value),可以修改已存在的响应头
		// response.setHeader("Content-Type", "text/json;charset=UTF-8");
		// 获取响应头的值getHeader
		String value = response.getHeader("company");
		System.out.println(value);// whsxt
		// 设置响应的MiME类型(内容类型),text/html表示服务器响应给浏览器一个html文档
		// response.setContentType("text/html");
		// 设置响应的编码
		// response.setCharacterEncoding("UTF-8");
		// 以上两句代码可以简写成一句
		//response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Content-Type", "text/html;charset=UTF-8");
		// 获取打印流
		PrintWriter out = response.getWriter();
		// 向浏览器输出中文内容
		out.print("欢迎学习servlet");
		// 刷新缓冲区
		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 {

		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();
	}

}

TestServlet.java

public class TestServlet extends HttpServlet {

	/**
	 * 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 {
   // response.sendRedirect(request.getContextPath()+ "/show.jsp");
		response.sendRedirect(request.getContextPath()+ "/req.action");
	}

	/**
	 * 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 {

		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();
	}

}

猜你喜欢

转载自blog.csdn.net/qq_41532872/article/details/87388245
今日推荐