Servlet学习笔记 实践
Servlet学习笔记 实践
目标:
1、看看Servlet开发、部署、请求、执行、响应全过程。
2、理解HTTP请求,并能熟练使用get、post请求。
实践:
一、开发Servlet
package test;
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 TestServlet extends HttpServlet {
/**
* 构造方法
*/
public TestServlet() {
super();
}
/**
* Servlet销毁方法. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doDelete method of the servlet. <br>
*
* 当收到一个HTTP delete请求的时候执行该方法.
*
* @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 doDelete(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* 当收到一个HTTP 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.setContentType("text/html");
request.setCharacterEncoding("GB18030");
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("<br>");
String x = request.getParameter("x");
String y = request.getParameter("y");
out.println("x="+x);
out.println("<br>");
out.println("y="+y);
out.println("<br>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* 当收到一个HTTP 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("<br>");
String x = request.getParameter("x");
String y = request.getParameter("y");
out.println("x="+x);
out.println("<br>");
out.println("y="+y);
out.println("<br>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPut method of the servlet. <br>
*
* 当收到一个HTTP put请求的时候执行该方法.
*
* @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 doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Put your code here
}
/**
* Returns information about the servlet, such as
* author, version, and copyright.
*
* @return String information about this servlet
*/
public String getServletInfo() {
return "This is my default servlet created by Eclipse";
}
/**
* Servlet初始化. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
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 TestServlet extends HttpServlet {
/**
* 构造方法
*/
public TestServlet() {
super();
}
/**
* Servlet销毁方法. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doDelete method of the servlet. <br>
*
* 当收到一个HTTP delete请求的时候执行该方法.
*
* @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 doDelete(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* 当收到一个HTTP 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.setContentType("text/html");
request.setCharacterEncoding("GB18030");
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("<br>");
String x = request.getParameter("x");
String y = request.getParameter("y");
out.println("x="+x);
out.println("<br>");
out.println("y="+y);
out.println("<br>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* 当收到一个HTTP 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("<br>");
String x = request.getParameter("x");
String y = request.getParameter("y");
out.println("x="+x);
out.println("<br>");
out.println("y="+y);
out.println("<br>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPut method of the servlet. <br>
*
* 当收到一个HTTP put请求的时候执行该方法.
*
* @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 doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Put your code here
}
/**
* Returns information about the servlet, such as
* author, version, and copyright.
*
* @return String information about this servlet
*/
public String getServletInfo() {
return "This is my default servlet created by Eclipse";
}
/**
* Servlet初始化. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
2、配置Servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
[url]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd[/url]">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>test.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/testServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
[url]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd[/url]">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>test.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/testServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3、访问Servlet
部署web应用并启动Servlet容器Tomcat。然后分别用两种方式来访问Servlet
a、通过URL直接访问
注意:第一参数x之前要用"?"号,其余参数之间用"&"符号。
显示效果:
b、通过一个JSP来访问
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
%>
<html>
<body>
test post Method<br>
<form action="<%=path %>/testServlet" method="post">
x:<input type="text" name="x"><br>
y:<input type="text" name="y"><br>
<input type="submit" value="submit">
<input type="reset" value="reset">
</form>
</body>
</html>
<%
String path = request.getContextPath();
%>
<html>
<body>
test post Method<br>
<form action="<%=path %>/testServlet" method="post">
x:<input type="text" name="x"><br>
y:<input type="text" name="y"><br>
<input type="submit" value="submit">
<input type="reset" value="reset">
</form>
</body>
</html>
如果将jsp中form的请求方式改为get,则点击submit按钮会显示:
二、简单总结下
HTTP请求有很多种,最常用的就是get和post。get请求的参数提交后会显示在url后面,而post请求不会。
get请求适合少量数据,post请求没有限制。
get请求参数会显示在rul后面,不安全。可以直接请求。
从安全角度考虑,应该使用post请求。
转载于:https://blog.51cto.com/trans/114569