当前位置:网站首页>记录:动态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同名
边栏推荐
- Excel导入,导出功能实现
- 1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
- [Chongqing Guangdong education] Shandong University College Physics reference materials
- Solution to the problem of automatic login in Yanshan University Campus Network
- 【无标题】
- 基本Dos命令
- [算法] 剑指offer2 golang 面试题2:二进制加法
- [算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
- The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
- 燕山大学校园网自动登录问题解决方案
猜你喜欢
FairyGUI摇杆
[algorithm] sword finger offer2 golang interview question 2: binary addition
[算法] 剑指offer2 golang 面试题10:和为k的子数组
Force buckle 1189 Maximum number of "balloons"
Unity3D制作注册登录界面,并实现场景跳转
FairyGUI循环列表
Liste des boucles de l'interface graphique de défaillance
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
[算法] 剑指offer2 golang 面试题1:整数除法
Matlab读取GNSS 观测值o文件代码示例
随机推荐
FairyGUI增益BUFF數值改變的顯示
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
RTKLIB: demo5 b34f.1 vs b33
wsl常用命令
FairyGUI简单背包的制作
FairyGUI条子家族(滚动条,滑动条,进度条)
Unity scene jump and exit
Mysql database index
What are the functions and features of helm or terrain
1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
FairyGUI循环列表
Devops' future: six trends in 2022 and beyond
Special palindromes of daily practice of Blue Bridge Cup
How to reduce the shutdown time of InnoDB database?
On March 15, the official version of go 1.18 was released to learn about the latest features and usage
[offer18] delete the node of the linked list
What is the maximum length of MySQL varchar field
[leetcode19] delete the penultimate node in the linked list
Latex learning
[leetcode15] sum of three numbers