当前位置:网站首页>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
边栏推荐
- RTKLIB: demo5 b34f. 1 vs b33
- Database table splitting strategy
- What are the advantages of using SQL in Excel VBA
- 闇の連鎖(LCA+树上差分)
- Knowledge system of digital IT practitioners | software development methods -- agile
- Prove the time complexity of heap sorting
- [algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
- [算法] 劍指offer2 golang 面試題2:二進制加法
- PRIDE-PPPAR源码解析
- Meanings and differences of PV, UV, IP, VV, CV
猜你喜欢
[algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
【干货】提升RTK模糊度固定率的建议之周跳探测
平衡二叉树详解 通俗易懂
C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
FairyGUI条子家族(滚动条,滑动条,进度条)
Wechat applet development experience
The port is occupied because the service is not shut down normally
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
Detailed explanation of balanced binary tree is easy to understand
随机推荐
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
Fabrication d'un sac à dos simple fairygui
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
Realization of the code for calculating the mean square error of GPS Height Fitting
第一人称视角的角色移动
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
Fairygui character status Popup
Unity3D,阿里云服务器,平台配置
【RTKLIB 2.4.3 b34 】版本更新简介一
[算法] 剑指offer2 golang 面试题2:二进制加法
FairyGUI增益BUFF數值改變的顯示
Problems and solutions of robust estimation in rtklib single point location spp
Usage differences between isempty and isblank
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
Lean product development - Lean Software Development & lean product development
Lock wait timeout exceeded try restarting transaction
基于rtklib源码进行片上移植的思路分享
[GNSS] robust estimation (robust estimation) principle and program implementation
Wechat applet development experience
FGUI工程打包发布&导入Unity&将UI显示出来的方式