当前位置:网站首页>记录:动态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同名
边栏推荐
- Itext 7 生成PDF总结
- Wechat applet development experience
- Unity3d, Alibaba cloud server, platform configuration
- (课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
- PRIDE-PPPAR源码解析
- Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
- Unity3D制作注册登录界面,并实现场景跳转
- Derivation of logistic regression theory
- RTKLIB: demo5 b34f.1 vs b33
- FairyGUI条子家族(滚动条,滑动条,进度条)
猜你喜欢
The port is occupied because the service is not shut down normally
音乐播放(Toggle && PlayerPrefs)
Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
Unity3d, Alibaba cloud server, platform configuration
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
FairyGUI按钮动效的混用
Unity场景跳转及退出
Fabrication d'un sac à dos simple fairygui
Excel导入,导出功能实现
FairyGUI簡單背包的制作
随机推荐
1041 be unique (20 points (s)) (hash: find the first number that occurs once)
Fairygui gain buff value change display
[offer29] sorted circular linked list
微信小程序开发心得
How to improve the deletion speed of sequential class containers?
[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length
(课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
FGUI工程打包发布&导入Unity&将UI显示出来的方式
SSD technical features
基本Dos命令
Prove the time complexity of heap sorting
What are the advantages of using SQL in Excel VBA
Idea problem record
[offer9] implement queues with two stacks
Combination of fairygui check box and progress bar
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
FairyGUI簡單背包的制作
InnoDB dirty page refresh mechanism checkpoint in MySQL
WSL common commands