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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝肚子胀不吃奶怎么办 宝宝25天不睡觉怎么办 50天婴儿不吃奶怎么办 儿子关在房间玩电脑怎么办? 宽带拨号上网账号密码忘了怎么办 双眼皮割的太宽怎么办 营业执照异常名录移除注销怎么办 工商局注册后骚扰电话怎么办 单位工作失误医保断交7年怎么办 大门对大门怎么办?巧用天官赐福 委托书公司名称打错了怎么办 招行ubank不对账怎么办 信贷公司利息高不合理怎么办 衣服上的logo掉怎么办 ui设计师接不到私活怎么办 微信打开很慢怎么办 小泰迪感冒加身上结痂怎么办 法斗眼睛肿了怎么办 地图鱼身上有白点怎么办 人被广告牌砸了怎么办 小米手机出现繁体中文英文怎么办 雅思考试把姓名写错了怎么办 房贷的流水账假怎么办 报到证报道期限过期了怎么办 注销公司公章丢了怎么办 家里的猫太调皮怎么办 孩子纹身了我该怎么办 46天婴儿感冒了怎么办 狗病了不吃东西怎么办 幼儿急诊见风了怎么办 哺乳期乳房有硬块而且疼怎么办 哺乳期乳头破裂乳房似针扎怎么办 回奶胀痛的厉害怎么办 淡水龟的壳变软了怎么办 棕色的泰迪变白怎么办 大班教案泥石流来了怎么办 鸟类的嘴巴坏了怎么办 四川麦蚊子咬了怎么办 脸上被晒脱皮了怎么办 额头被晒脱皮了怎么办 脸黑一块白一块怎么办