Servlet 配置文件web.xml
来源:互联网 发布:软件加注册码工具 编辑:程序博客网 时间:2024/05/22 17:37
一、配置文件如下:
<?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 hereSystem.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对象 全局所有的页面有效;ServletContextapplication = 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 hereSystem.out.println("初始化!");}}
0 0
- Servlet配置文件web.xml
- Servlet 配置文件web.xml
- servlet web.xml配置文件
- servlet 的web.xml配置文件中的介绍
- Java Servlet 配置文件 web.xml 详解
- web.xml配置文件中的servlet和servlet-mapping
- web.xml配置文件中的servlet和servlet-mapping
- web.xml配置文件中的servlet和servlet-mapping
- SpringMVC(1):基础配置文件web.xml 和 <servlet-name>-servlet.xml / springmvc.xml
- web.xml配置文件中的Servlet和filter区别?
- blog-servlet.xml配置文件
- spring-servlet.xml配置文件
- Webwork2 WEB.xml配置文件
- Tomcat 配置文件web.xml
- web.xml 配置文件详解
- web.xml 配置文件
- web.xml配置文件总结
- web.xml配置文件
- 【Unity】AssetBundle 资源打包和依赖资源打包
- shell中$的用法
- 将文件路径以"\"隔开
- C#获取网站源码的两种方式,不乱码
- oracle只读模式
- Servlet 配置文件web.xml
- MySQL常见错误
- UML学习之序列图
- c++类的构造函数详解
- bootstrap引用glyphicon图标无法显示
- jquery以及js中那些好用的函数
- Wix Toolset总结
- 关于getch()的一点小知识
- 山顶点的提取