原始的servlet方式
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="jquery-3.3.1.js"></script>
<script type="text/javascript">
</script>
</head>
<body>
<form action="<%=basePath%>/test3.htm" method="post">
<p>用户名:<input type="text" name="user"></p>
<p>密 码:<input type="password" name="pwd"></p>
<p>年 龄:<input type="text" name="nianling"></p>
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>
@RequestMapping(value = "/test3")
public void test3(String user, String pwd, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
if ("admin".equals(user) && "123".equals(pwd)) {//登录成功
// request.getRequestDispatcher("/pages/front/success.jsp").forward(request, response);
String basePath = request.getContextPath();
response.sendRedirect(basePath + "/pages/front/success.jsp");
}else {//失败
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
成功
失败
springMVC中的重定向与转发
转发
在return后面直接输入跳转地址或者forward:+地址(建议多用)
@RequestMapping(value = "/test4")
public String test4(String user, String pwd){
if ("admin".equals(user) && "123".equals(pwd)) {//登录成功
//spring的重定向
return "redirect:/pages/front/success.jsp";
}else {//失败
//spring的转发,不写forward默认为转发
// return "forward:/pages/front/fail.jsp";
return "/pages/front/fail.jsp";
}
}
重定向
在return后面redirect:+地址
视图解析器
我们发现在转发和重定向时地址反复填写,特别麻烦,所以视图解析器发挥作用了!
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<!-- 配置包扫描 -->
<context:component-scan base-package="cn.java.controller.*"></context:component-scan>
<!-- 配置mvc特有的注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀 -->
<property name="prefix" value="/pages/"></property>
<!-- 配置后缀 -->
<!-- <property name="suffix" value=".jsp"></property> -->
</bean>
</beans>
其实我们可以将前缀和后缀写的更详细的,但是那样代码可读性会很差,
@RequestMapping(value = "/test4")
public String test4(String user, String pwd){
if ("admin".equals(user) && "123".equals(pwd)) {//登录成功
//spring的重定向
return "redirect:/pages/front/success.jsp";
}else {//失败
//spring的转发,不写forward默认为转发
// return "forward:/pages/front/fail.jsp";
return "/pages/front/fail.jsp";
}
}
//视图解析器只对转发有效对重定向无效
@RequestMapping(value = "/test5")
public String test5(String user, String pwd){
System.out.println(user+pwd);
if ("admin".equals(user) && "123".equals(pwd)) {//登录成功
return "front/success.jsp";
}else {//失败
return "front/fail.jsp";
}
}
并且视图解析器支队转发有效,这也是我建议使用转发的原因!
中文乱码问题
servlet中的解决办法
用filter过滤器来解决:
package cn.java.filters;
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;
import javax.servlet.annotation.WebFilter;
/**
* Servlet Filter implementation class EncodingFilter
*/
@WebFilter(filterName="EncodingFilter",urlPatterns="/*")
public class EncodingFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text.html;charset=utf-8");
chain.doFilter(request, response);
}
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
spring中的解决办法
过滤器一定要配置在servlet前面
在web.xml中配置filter
<!-- 配置过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>