转servlet web.xml配置

通过以下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的理解,我觉得很简单,但真正写程序的时候部署程序是非常复杂的

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值