表单获取页面,名为SchoolResult001jsp,使用HttpSession获取值(使用JSP内置对象和EL表达式两种方式) 使用过滤器完成中文编码,文件名为EncodingFilter001.java。 使用过滤器完成“备注“过滤功能,文件名为CharacterFilter001.java,将“备注”中的【好】过滤完成后,使用【棒】输出
时间: 2024-03-23 16:37:01 浏览: 68
好的,让我为您提供一些关于如何实现您的要求的指导。
1. 在SchoolResult001.jsp页面中,使用HttpSession来获取表单信息。您可以使用JSP内置对象session来获取session对象,并使用EL表达式获取JavaBean对象中的属性值。例如,${sessionScope.school.sname}表示获取session中名为school的JavaBean对象的sname属性值。
2. 创建一个名为EncodingFilter001.java的过滤器,用于完成中文编码。在该过滤器中,您需要使用request.setCharacterEncoding方法将请求的编码设置为UTF-8。
3. 创建一个名为CharacterFilter001.java的过滤器,用于完成“备注”中的过滤功能。在该过滤器中,您需要使用request.getParameter方法获取“备注”参数值,并使用String的replace方法将其中的“好”替换为“棒”。
4. 将EncodingFilter001.java和CharacterFilter001.java文件放置在src目录下,并在web.xml文件中配置过滤器。
示例代码如下:
SchoolResult001.jsp页面:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>School Result</title>
</head>
<body>
<h1>School Result</h1>
<p>学校名:${sessionScope.school.sname}</p>
<p>学校性质:${sessionScope.school.nature}</p>
<p>学科分类:${sessionScope.school.major}</p>
<p>学生人数:${sessionScope.school.num}</p>
<p>备注:${sessionScope.school.message}</p>
</body>
</html>
```
EncodingFilter001.java过滤器:
```java
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class EncodingFilter001 implements Filter {
private String encoding;
public void init(FilterConfig config) throws ServletException {
encoding = config.getInitParameter("encoding");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}
public void destroy() {
}
}
```
CharacterFilter001.java过滤器:
```java
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharacterFilter001 implements Filter {
private String target;
private String replacement;
public void init(FilterConfig config) throws ServletException {
target = config.getInitParameter("target");
replacement = config.getInitParameter("replacement");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
String message = request.getParameter("message");
message = message.replace(target, replacement);
request.setAttribute("message", message);
chain.doFilter(request, response);
}
public void destroy() {
}
}
```
web.xml文件中的过滤器配置:
```xml
<filter>
<filter-name>EncodingFilter001</filter-name>
<filter-class>EncodingFilter001</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter001</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>CharacterFilter001</filter-name>
<filter-class>CharacterFilter001</filter-class>
<init-param>
<param-name>target</param-name>
<param-value>好</param-value>
</init-param>
<init-param>
<param-name>replacement</param-name>
<param-value>棒</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterFilter001</filter-name>
<url-pattern>/SchoolServlet001</url-pattern>
</filter-mapping>
```
希望这些代码能够对您有所帮助!
阅读全文