Java Servlet的配置文件web.xml配置内容和具体含义
2013-08-13 10:36
411 查看
虽然在Servlet3.0标准中不再使用web.xml配置servlet文件,但是还是很有必要理解下servlet的配置方式,因为即使是用annotation其本质还是一种配置。
文章转载至:http://blog.sina.com.cn/s/blog_4a2061060100b9pb.html。感谢楼主~
package cn.mldn.lxh.servlet;//定义包
import java.io.*;
import javax.servlet.*;//导入HttpServlet所属的包
//HttpServletRequest、HttpServletResponse存放在javax.servlet.http包下
importjavax.servlet.http.* ;
public class SimpleServlet extendsHttpServlet
{
// 表示处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse resp)throws IOException,ServletException
//抛出异常
{
PrintWriter out = resp.getWriter();//实例化out对象。
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>THE FIRSTSERVLET</TITLE>") ;
out.println("</HEAD>");
out.println("<BODY>");
out.println("<H1>HelloWorld!!!</H1>") ;
out.println("</BODY>");
out.println("</HTML>");
out.close() ;
}
public void doPost(HttpServletRequest req,HttpServletResponse resp)throws IOException,ServletException
{
this.doGet(request,response) ;
}
};
///////////////////////////////////////////////////////////////////////////////////////////
JSP功能和servlet一致,也就是说servlet可以被外部访问,那么要访问它就需要通过一个地址,因此只有通过WEB的地址映射来解决了。
如何进行地址映射呢?
通过修改web.xml文件进行。
代码如下:
<servlet>
<servlet-name>simple</servlet-name>//我们定义的servlet应用名字
<servlet-class>cn.mldn.lxh.servlet.SimpleServlet</servlet-class>//我们定义的servlet应用名字对应的具体servlet文件
</servlet>
<servlet-mapping> //地址映射
<servlet-name>simple</servlet-name>//我们定义的servlet应用名字
<url-pattern>/demo</url-pattern>//地址名
</servlet-mapping>
////////////////////////////////////////////////////////////////////////////////////////////
进行地址映射目的是为了使用servlet,它的工作过程是:
工作过程:
输入地址:http://localhost:8080/
demo,通过它找到映射文件内部的文件名simple,通过simple找到对应的<servlet-name>simple</servlet-name>,然后定位到这个servlet文件:cn.mldn.lxh.servlet.SimpleServlet
由此可见,<servlet-name>simple</servlet-name>中的名字simple不一定一定要与servlet的文件名“SimpleServlet.java”一致,它只是在对web.xml文件配置的时候我们对servlet应用名字的定义,通过<servlet-class>cn.mldn.lxh.servlet.SimpleServlet</servlet-class>我们就可以定位到这个servlet文件。
文章转载至:http://blog.sina.com.cn/s/blog_4a2061060100b9pb.html。感谢楼主~
package cn.mldn.lxh.servlet;//定义包
import java.io.*;
import javax.servlet.*;//导入HttpServlet所属的包
//HttpServletRequest、HttpServletResponse存放在javax.servlet.http包下
importjavax.servlet.http.* ;
public class SimpleServlet extendsHttpServlet
{
// 表示处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse resp)throws IOException,ServletException
//抛出异常
{
PrintWriter out = resp.getWriter();//实例化out对象。
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>THE FIRSTSERVLET</TITLE>") ;
out.println("</HEAD>");
out.println("<BODY>");
out.println("<H1>HelloWorld!!!</H1>") ;
out.println("</BODY>");
out.println("</HTML>");
out.close() ;
}
public void doPost(HttpServletRequest req,HttpServletResponse resp)throws IOException,ServletException
{
this.doGet(request,response) ;
}
};
///////////////////////////////////////////////////////////////////////////////////////////
JSP功能和servlet一致,也就是说servlet可以被外部访问,那么要访问它就需要通过一个地址,因此只有通过WEB的地址映射来解决了。
如何进行地址映射呢?
通过修改web.xml文件进行。
代码如下:
<servlet>
<servlet-name>simple</servlet-name>//我们定义的servlet应用名字
<servlet-class>cn.mldn.lxh.servlet.SimpleServlet</servlet-class>//我们定义的servlet应用名字对应的具体servlet文件
</servlet>
<servlet-mapping> //地址映射
<servlet-name>simple</servlet-name>//我们定义的servlet应用名字
<url-pattern>/demo</url-pattern>//地址名
</servlet-mapping>
////////////////////////////////////////////////////////////////////////////////////////////
进行地址映射目的是为了使用servlet,它的工作过程是:
工作过程:
输入地址:http://localhost:8080/
demo,通过它找到映射文件内部的文件名simple,通过simple找到对应的<servlet-name>simple</servlet-name>,然后定位到这个servlet文件:cn.mldn.lxh.servlet.SimpleServlet
由此可见,<servlet-name>simple</servlet-name>中的名字simple不一定一定要与servlet的文件名“SimpleServlet.java”一致,它只是在对web.xml文件配置的时候我们对servlet应用名字的定义,通过<servlet-class>cn.mldn.lxh.servlet.SimpleServlet</servlet-class>我们就可以定位到这个servlet文件。
相关文章推荐
- Java Servlet的配置文件web.xml配置内容和具体含义
- Java Servlet的配置文件web.xml配置内容和具体含义
- Java Servlet的配置文件web.xml配置内容和具体含义
- Java Servlet 配置文件 web.xml 详解
- java配置文件web.xml中参数设置及含义
- load-on-startup在配置文件web.xml中的含义
- Java - web.xml文件中可以配置哪些内容?
- SSM(spring-springmvc-mybatis)整合开发xml配置文件内容springmvc-config.xml,application.xml,web.xml
- shh整合后web.xml、spring配置文件和struts.xml的内容
- shh整合后web.xml、spring配置文件和struts.xml的内容
- web.xml中引入SPRING配置文件classpath的含义
- web.xml配置文件祥解
- xml读取properties配置文件,web.xml读取properties,properties格式,properties在eclipse创建
- web.xml文件配置
- web.xml配置文件的加载顺序
- web.xml文件中引用log4J的配置以及log4J文件配置
- 为Struts应用配置web.xml文件
- web.xml配置文件详解(转)
- servlet web.xml配置文件
- java web开发中 web.xml文件的作用及常用配置说明