随笔 - 20  文章 - 57  trackbacks - 0
<2010年12月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

51CTO

搜索

  •  

最新评论

阅读排行榜

评论排行榜

通过以下Servlet程序和web.xml来说明web.xml的配置以及过程


创建一个Login的HTML文件



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>login.html</title>
   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
 
 </head>
  <body>
    <form action="test1" method="post">
      <table border="0" width="379" height="79">
        <tr>
          <td>帐号:</td>
          <td><input type="text" name="username"></td>
        </tr>
        <tr>
          <td>密码:</td>
          <td><input type="password" name="password"></td>
        </tr>
        <tr>
          <td colspan="5" align="center"><input type="submit" value="登录"></td>
        </tr>
      </table>
    </form>
  </body>
</html>



以上HTML标签中要说明的是:
<form>标签中的 action="test_Web_xml" 和 method="post" 分别定义了Html将登陆的信息发送给了谁,以及发送信息的方法!


创建一个Servlet程序

public class LoginServlet extends HttpServlet{
   
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        String username = null;
        username = request.getParameter("username");
        String password = null;
        password = request.getParameter("password");
       
        if(username.equals("username")&&password.equals("password")){
            request.getRequestDispatcher("成功登陆!!!").forward(request,response);
        }else{
            request.getRequestDispatcher("登陆失败!!!").forward(request,response);   
        }
    }
}



web.xml配置


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    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_2_5.xsd">
  
   
    <servlet>
      <servlet-name>
      Login
      </servlet-name>
     
      <servlet-class>
        com.rise.LoginServlet
        </servlet-class>
     </servlet>
 
 
     <servlet-mapping>
      <servlet-name>
       Login
      </servlet-name>
      <url-pattern>
      /test1

      </url-pattern>

     </servlet-mapping>
   
   
   
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>



我理解web.xml的作用就是将页面和后台程序正确连接!!!



通过一张图片说明我理解的web.xml的作用






以上的内容是我自己对web.xml的理解,我觉得很简单,但真正写程序的时候部署程序是非常复杂的!

posted on 2010-12-11 20:43 tovep 阅读(6277) 评论(2)  编辑  收藏

FeedBack:
# re: Servlet中web.xml配置 2010-12-12 19:27 爱品者
楼主说了一部分,还有更重要的就是非授权跳转,光靠servlet是不够的,必须有filter配合  回复  更多评论
  
# re: Servlet中web.xml配置 2011-11-09 16:45 质量
讲的很好  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
主页