当前位置:网站首页>记录:动态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同名
边栏推荐
- Teach you to release a DeNO module hand in hand
- Unity3D制作注册登录界面,并实现场景跳转
- 基本Dos命令
- [算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
- How to reduce the shutdown time of InnoDB database?
- What are the functions and features of helm or terrain
- Idea problem record
- Prove the time complexity of heap sorting
- FairyGUI增益BUFF數值改變的顯示
- FairyGUI循環列錶
猜你喜欢
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
Office prompts that your license is not genuine pop-up box solution
微信小程序开发心得
FairyGUI复选框与进度条的组合使用
Vulnhub target: hacknos_ PLAYER V1.1
Liste des boucles de l'interface graphique de défaillance
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
Fairygui joystick
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
随机推荐
[rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
2021.11.10 compilation examination
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
(课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
第一人称视角的角色移动
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
基于rtklib源码进行片上移植的思路分享
[leetcode622] design circular queue
[Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
2022国赛Re1 baby_tree
(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
Unity scene jump and exit
Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
How to reduce the shutdown time of InnoDB database?
Mixed use of fairygui button dynamics
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
[offer29] sorted circular linked list
341. Flatten nested list iterator