一、配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<!-- 全局参数 -->
<context-param>
<param-name>names</param-name>
<param-value>游将军</param-value>
</context-param>
<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>com.hlx.test.TestServlet</servlet-class>
<!--某一个servlet配置初始化参数 -->
<init-param>
<param-name>username</param-name>
<param-value>yjj</param-value>
</init-param>
</servlet>
<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>LoginServlet</servlet-name>
<servlet-class>com.hlx.login.LoginServlet</servlet-class>
</servlet>
<!-- URL名称可以任意写 -->
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<!-- <servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/my/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
-->
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
二、TestServlet页面
public class TestServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public TestServlet() {
super();
System.out.println("实例化对象!");
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
System.out.println("被火化了!");
}
/**
* The doGet method of the servlet. <br>
* 相当于表单提交的默认格式:method="GET"
*
* 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.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
System.out.println("处理doGet方法!");
out.println("欢迎使用~==>" + this.getInitParameter("username") + "!");
// 调用全局配置参数值
String uname = this.getServletContext().getInitParameter("names");
//out.println(uname);
//保存值
request.setAttribute("unames", uname);
//间接跳转 -->转发
//request.getRequestDispatcher("login").forward(request, response);
//直接跳转-->重定向
//保存值 application 全局所有的页面有效;
//获得session对象
HttpSession session= request.getSession();
session.setAttribute("usession", uname);
//获得application对象 全局所有的页面有效;
ServletContext application = this.getServletContext();
application.setAttribute("uapp", uname);
// response.sendRedirect("login");
out.println("<p/>请求路径:"+request.getContextPath());
out.println("<p/>请求方法:"+request.getMethod());
out.println("<p/>请求Addr:"+request.getLocalAddr());
out.println("<p/>请求post:"+request.getLocalPort());
out.println("<p/>请求Name:"+request.getLocalName());
out.println("<p/>请求Addr:"+request.getRemoteAddr());
out.println("<p/>请求URI:"+request.getRequestURI());
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
* 表单写提交方式:method="post"
*
* 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();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
System.out.println("初始化!");
}
}