带servlet ,filter字符转换的

    xiaoxiao2021-03-25  129

    index.jsp

    <body>   <form action="<%=request.getContextPath() %>/AddServlet" method="post">   <table align="center" border="1" width="330">   <tr><td align="center" colspan="2">   </td></tr>   <tr>   <td> 图书编号</td>   <td><input type="text" name="id"/></td>   </tr>   <tr> <td>图书名称</td>   <td><input type="text" name="name"/></td>   </tr>   <tr><td align="center" colspan="2">       <input type="submit" value="submit"/>       </td>       </tr>       </table>       </form>    </body>

    AddServlet.java

    public class AddServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out=response.getWriter(); String id=request.getParameter("id"); String name=request.getParameter("name"); out.print("图书编号:"+id+"<br>"); out.println("图书名称"+name+"<br>"); out.flush(); out.close(); }

    CharactorFilter .java

    public class CharactorFilter implements Filter { String encoding = null; @Override public void destroy() { encoding = null; } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // 判断字符编码是否为空 if(encoding != null){ // 设置request的编码格式 request.setCharacterEncoding(encoding); // 设置response字符编码       response.setContentType("text/html; charset="+encoding); } // 传递给下一过滤器 chain.doFilter(request, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { // 获取初始化参数 encoding = filterConfig.getInitParameter("encoding"); } } }

    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">   <display-name>Charactor</display-name>   <welcome-file-list>     <welcome-file>index.html</welcome-file>     <welcome-file>index.htm</welcome-file>     <welcome-file>index.jsp</welcome-file>     <welcome-file>default.html</welcome-file>     <welcome-file>default.htm</welcome-file>     <welcome-file>default.jsp</welcome-file>   </welcome-file-list>         <filter>    <filter-name>CharactorFilter</filter-name>    <filter-class>com.atguigu.charactor.CharactorFilter</filter-class>    <init-param>    <param-name>encoding</param-name>    <param-value>UTF-8</param-value>    </init-param>    </filter>        <filter-mapping>       <filter-name>CharactorFilter</filter-name>       <url-pattern>/*</url-pattern>   </filter-mapping>         <servlet>     <servlet-name>AddServlet</servlet-name>     <servlet-class>com.atguigu.charactor.AddServlet</servlet-class>   </servlet>   <servlet-mapping>     <servlet-name>AddServlet</servlet-name>     <url-pattern>/AddServlet</url-pattern>   </servlet-mapping>                 </web-app>

    是3.0版本的,建的是servlet,filter 但是还是把自带的隐射去掉在web.xml中写

    转载请注明原文地址: https://ju.6miu.com/read-3085.html

    最新回复(0)