当前位置:网站首页>记录:动态Web项目servlet访问数据库404错误之解决
记录:动态Web项目servlet访问数据库404错误之解决
2022-07-06 09:18:00 【非风之想】
动态Web项目servlet访问数据库404错误之解决
login.html
在login.html 在浏览器中显示的页面输入用户用和密码点击登录,根据action/Web/login转到Web项目下web.xml文件。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/Web/login" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录"><br>
</form>
</body>
</html>
web.xml
在xml文件中的servlet-mapping标签中的url-pattern标签中的/login,找到/login之后,根据上面的servlet-name标签中文本LoginServlet去找servlet标签中servlet-name标签中的相同文本LoginServlet,然后找到下面servlet-class标签中的文本login.LoginServlet,该文本表示要运行.java文件所在的路径 (所在的包)。
<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>login.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
LoginServlet.java
根据xml中的路径(所在的包)来运行LoginServlet.java文件,从而操作数据库中的数据
package login;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import domain.User;
import utils.DataSourceUtils;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获得用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//2.从数据库中验证该用户名和密码是否真缺
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from user where username=? and password=?";
User user = null;
try {
user = runner.query(sql, new BeanHandler<User>(User.class), username,password);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//3.根据返回的结果给用户不同的显示信息
if (user!=null) {
//用户登录成功
response.getWriter().write(user.toString());
}else {
//用户登陆失败
response.getWriter().write("sorry your username or password is wriong!!!");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
所以web.xml 中的url-mapping文本/login中的login 必须与form标签的action=/Web/login中的login同名
边栏推荐
- 【GNSS】抗差估计(稳健估计)原理及程序实现
- [Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
- PRIDE-PPPAR源码解析
- 【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
- MySQL error warning: a long semaphore wait
- [算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
- 地球围绕太阳转
- [algorithm] sword finger offer2 golang interview question 7: 3 numbers with 0 in the array
- Halcon knowledge: gray_ Tophat transform and bottom cap transform
- 【干货】提升RTK模糊度固定率的建议之周跳探测
猜你喜欢

使用rtknavi进行RT-PPP测试

FairyGUI条子家族(滚动条,滑动条,进度条)
![[算法] 剑指offer2 golang 面试题1:整数除法](/img/e6/f17135207b3540ec58e5a9eed54220.png)
[算法] 剑指offer2 golang 面试题1:整数除法

Guided package method in idea

Office提示您的许可证不是正版弹框解决

染色法判定二分图
![[algorithm] sword finger offer2 golang interview question 1: integer division](/img/e6/f17135207b3540ec58e5a9eed54220.png)
[algorithm] sword finger offer2 golang interview question 1: integer division
![[算法] 剑指offer2 golang 面试题10:和为k的子数组](/img/63/7422489d09a64ec9f0e79378761bf1.png)
[算法] 剑指offer2 golang 面试题10:和为k的子数组

Combination of fairygui check box and progress bar
![[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K](/img/8c/1b6ba3b1830ad28176190170c98628.png)
[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K
随机推荐
IText 7 generate PDF summary
基于rtklib源码进行片上移植的思路分享
Derivation of logistic regression theory
Force buckle 1189 Maximum number of "balloons"
Wechat applet development experience
编辑距离(多源BFS)
idea中好用的快捷键
Usage differences between isempty and isblank
Office prompts that your license is not genuine pop-up box solution
Fairygui character status Popup
染色法判定二分图
(the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
Fabrication d'un sac à dos simple fairygui
idea中导包方法
InnoDB dirty page refresh mechanism checkpoint in MySQL
Mixed use of fairygui button dynamics
Idea problem record
isEmpty 和 isBlank 的用法区别
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
rtklib单点定位spp使用抗差估计遇到的问题及解决