MyEclipse——过滤器中html页面无法跳转运行

        今天弄了过滤器的实验,一直就是运行,但无法跳转,情况如下:

 

 

 

这个多试几次就好了。注意上一篇提到的servlet中的地址映射要一致

1、添加过滤器代码:

 

2、在之前创建的servlet基础上的,添加以下东西:

参考实验如:

编写一个过滤器改变请求编码。

【步骤 1】编写一个 loginform.html 文件,代码如下:

<!DOCTYPE html>

<html>

  <head>

    <title>loginform.html</title>



    <meta name="keywords" content="keyword1,keyword2,keyword3">

    <meta name="description" content="this is my page">

    <meta name="content-type" content="text/html; charset=GB18030">

    

    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->



  </head>

  

  <body>

    <center>

  <h2>请输入用户名和口令:</h2>

  <form method="post" action="servlet/CheckParamServlet">

  <table>

    <tr>

       <td>用户名:</td>

       <td><input name="name" type="text"></td>

   </tr>

   <tr>

      <td>口 令:</td>

      <td><input name="pass" type="password"></td>

   </tr>

   <tr>

        <td></td>

         <td>

           <input name="ok" type="submit" value="提交">

           <input name="cancel" type="reset" value="重置">

         </td>

    </tr>

  </table>

  </form>

  </center>

  </body>

</html>

【步骤 2】编写处理请求参数的 Servlet,代码如下:

package hu;



import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

@SuppressWarnings("serial")

public class CheckParamServlet extends HttpServlet{

 public void doGet(HttpServletRequest request,

 HttpServletResponse response)

 throws ServletException, IOException {

 String name = request.getParameter("name");

 String pass = request.getParameter("pass");

 response.setContentType("text/html;charset=gb2312");

 PrintWriter out = response.getWriter();



 out.println("<html><head><title>Param Test</title></head>");

 out.println("<h3 align=center>你的用户名为:"+name+"</h3>");

 out.println("<h3 align=center>你的口令为:"+pass+"</h3>");

 out.println("</body></html>");

 }



 public void doPost(HttpServletRequest request,

 HttpServletResponse response)

 throws ServletException, IOException {



 doGet(request,response);

 }

}

【步骤 3】修改 web.xml 文件,加入下面代码:

<?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">

  <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>CheckParamServlet</servlet-name>

    <servlet-class>hu.CheckParamServlet</servlet-class>

  </servlet>



  <servlet-mapping>

    <servlet-name>CheckParamServlet</servlet-name>

    <url-pattern>/servlet/CheckParamServlet</url-pattern>

  </servlet-mapping>



</web-app>

【步骤 4】在浏览器的地址栏中输入下面 URL:

 http://localhost:8080/ helloapp/loginform.html

 输入用户名和口令,如下图所示:

图 14.1 loginform.html 页面的运行结果

 

 

 

然后点击“提交”按钮,经 CheckParamServlet 处理后返回的结果如下图所示::

图 14.2 Check程序的运行结果

 

从这里我们可以看到,从服务器返回的汉字成了乱码。原因是没有指定 request 的编

码。

下面通过编写一个过滤器改变请求编码。

【步骤 5】过滤器代码如下:

 

【步骤 6】在 web.xml 文件中配置过滤器,加入下面代码:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

  <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>CheckParamServlet</servlet-name>

    <servlet-class>hu.CheckParamServlet</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>CheckParamServlet</servlet-name>

    <url-pattern>/servlet/CheckParamServlet</url-pattern>

  </servlet-mapping>




以下是添加的代码:
  <filter>

    <filter-name>guo</filter-name>

    <filter-class>filter.guo</filter-class>

    <init-param>

      <param-name>Encoding</param-name>

      <param-value>gb2312</param-value>

    </init-param>

  </filter>

  <filter-mapping>

    <filter-name>guo</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

</web-app>



【步骤 7】重复第(4)步操作,结果如下:

EncodingFilter 程序的运行结果

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值