JavaWeb技术(二) -- web工程常见问题及解决方案
上一篇我们已经用eclipse搭建好了一个maven工程,并转换成了web工程,现在我们讨论web工程中较常见的问题
1、jsp页面中文乱码问题
当我们将创建好的web工程部署到tomcat上,并在浏览器上访问 http://localhost:8080/MyWeb/ 时,发现页面显示的是乱码,index.jsp 内容如下
<html>
<body>
<h2>首页</h2>
</body>
</html>
解决方法是在index.jsp中添加 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html;charset=utf-8"%>,修改后的index.jsp内容如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"
contentType="text/html;charset=utf-8"%>
<html>
<body>
<h2>首页</h2>
</body>
</html>
2、创建一个servlet ,并通过servlet配置的url访问该servlet

(1)新建一个类LoginServlet,继承HttpServlet,并重写doGet和doPost方法,并将请求转向jsp页面,如下
package com.fanghao;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 8859563008113458339L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置请求要跳转到的页面为/WEB-INF/jsp/login.jsp,此处采用的是相对路径,webapp文件夹就相当于是工程根路径
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
(2)在web.xml中配置该servlet,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">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.fanghao.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
每一个servlet都需要配置一组<servlet>和<servlet-mapping>,如果有多个servlet,就配置多组,其中的<url-pattern>配置的就是访问该servlet使用的url
(3)在工程的WEB-INF文件夹下的jsp文件夹下创建一个login.jsp即可,login.jsp内容如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"
contentType="text/html;charset=utf-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h2>欢迎访问登录页面</h2>
</body>
</html>
(3)实现servlet和jsp页面之间的相互跳转

修改以上的login.jsp,内容如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"
contentType="text/html;charset=utf-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/doLoginCheck" method="post">
用户名:<input type="text" name="username">
<input type="submit" value="提交" id="submitBtn">
</form>
</body>
</html>
其中form表单的action属性表示的就是当我们点击提交按钮时,该表单的内容被提交到哪个url地址,${pageContext.request.contextPath }是使用EL表达式取出pageContext.request.contextPath表示的值,就是当前工程名,即对于当前项目而言 ${pageContext.request.contextPath } 等价于 /MyWeb
我们可以看到login.jsp出现红色报错:Multiple annotations found at this line:- javax.servlet.jsp cannot be resolved to a type
这是因为工程缺少servlet-api.jar和jsp-api.jar,添加这两个jar包即可解决,对应的maven依赖为
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
新建类LoginCheckServlet,继承HttpServlet,并重写doGet和doPost方法
package com.fanghao;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class LoginCheckServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//客户端是以UTF-8编码传输数据到服务器端的,所以需要设置服务器端以UTF-8的编码方式接收,否则对于中文数据就会产生乱码
request.setCharacterEncoding("UTF-8");
//request.getParameter的key是与jsp页面上的输入框input的name属性一一对应的
String userName = request.getParameter("username");
System.out.println("用户登录验证通过,向数据库中插入数据:" + userName);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
在web.xml中添加该servlet的配置
<?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">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.fanghao.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>loginCheckServlet</servlet-name>
<servlet-class>com.fanghao.LoginCheckServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>loginCheckServlet</servlet-name>
<url-pattern>/doLoginCheck</url-pattern>
</servlet-mapping>
</web-app>
以上就实现了servlet和jsp页面之间的相互跳转,可同理增加jsp页面和servlet实现更为复杂的程序

浙公网安备 33010602011771号