当前位置:网站首页>Record: solution of 404 error of servlet accessing database in dynamic web project
Record: solution of 404 error of servlet accessing database in dynamic web project
2022-07-06 13:00:00 【Non wind thought】
dynamic Web project servlet Access database 404 Error resolution
login.html
stay login.html Enter the user's password and click login on the page displayed in the browser , according to action/Web/login go to Web Under the project web.xml file .
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/Web/login" method="post">
user name :<input type="text" name="username"><br>
password :<input type="password" name="password"><br>
<input type="submit" value=" Sign in "><br>
</form>
</body>
</html>
web.xml
stay xml In the document servlet-mapping In the tag url-pattern In the tag /login, find /login after , According to the above servlet-name Text in label LoginServlet Look for servlet In the label servlet-name The same text in the label LoginServlet, Then find the following servlet-class The text in the label login.LoginServlet, This text indicates to run .java The path of the file ( The bag where it is ).
<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
according to xml In the path ( The bag where it is ) To run the LoginServlet.java file , So as to operate the data in the database
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. Get user name and password
String username = request.getParameter("username");
String password = request.getParameter("password");
//2. Verify whether the user name and password are missing from the database
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 Automatically generated catch block
e.printStackTrace();
}
//3. Different display information is given to users according to the returned results
if (user!=null) {
// User login successful
response.getWriter().write(user.toString());
}else {
// User login failed
response.getWriter().write("sorry your username or password is wriong!!!");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
therefore web.xml Medium url-mapping Text /login Medium login Must be with form Labeled action=/Web/login Medium login The same name
边栏推荐
- 雇佣收银员【差分约束】
- Halcon knowledge: gray_ Tophat transform and bottom cap transform
- KF UD decomposition pseudo code implementation advanced [2]
- Unity3d makes the registration login interface and realizes the scene jump
- Role movement in the first person perspective
- 【GNSS】抗差估计(稳健估计)原理及程序实现
- 错误:排序与角标越界
- Fairygui loop list
- Fundamentals of UD decomposition of KF UD decomposition [1]
- Fairygui gain buff value change display
猜你喜欢

Office提示您的许可证不是正版弹框解决
![[算法] 剑指offer2 golang 面试题4:只出现一次的数字](/img/f7/23ffc81ec8e9161c15d863c1a67916.png)
[算法] 剑指offer2 golang 面试题4:只出现一次的数字

Wechat applet development experience

The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan

【干货】提升RTK模糊度固定率的建议之周跳探测

MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列

The earth revolves around the sun

FairyGUI人物状态弹窗
![[algorithm] sword finger offer2 golang interview question 1: integer division](/img/e6/f17135207b3540ec58e5a9eed54220.png)
[algorithm] sword finger offer2 golang interview question 1: integer division

Force buckle 1189 Maximum number of "balloons"
随机推荐
[算法] 剑指offer2 golang 面试题1:整数除法
Employment of cashier [differential constraint]
Code example of MATLAB reading GNSS observation value o file
On March 15, the official version of go 1.18 was released to learn about the latest features and usage
Fairygui gain buff value change display
RTKLIB: demo5 b34f.1 vs b33
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
Heap sort [handwritten small root heap]
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
MySQL performance tuning - dirty page refresh
记录:下一不小心写了个递归
音乐播放(Toggle && PlayerPrefs)
Agile development helps me
KF UD分解之伪代码实现进阶篇【2】
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
Role movement in the first person perspective
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers