当前位置:网站首页>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
边栏推荐
- FairyGUI增益BUFF數值改變的顯示
- Employment of cashier [differential constraint]
- How to improve the deletion speed of sequential class containers?
- 抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
- Rt-ppp test using rtknavi
- 错误: 找不到符号
- Edit distance (multi-source BFS)
- Pride-pppar source code analysis
- 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
- Role movement in the first person perspective
猜你喜欢
[algorithm] sword finger offer2 golang interview question 1: integer division
[算法] 剑指offer2 golang 面试题2:二进制加法
Teach you to release a DeNO module hand in hand
[Chongqing Guangdong education] Shandong University College Physics reference materials
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
The earth revolves around the sun
[untitled]
The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
Idea problem record
FairyGUI循環列錶
随机推荐
IText 7 generate PDF summary
Unity3D,阿里云服务器,平台配置
记录:newInstance()过时的代替方法
Pride-pppar source code analysis
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
Agile development helps me
Realization of the code for calculating the mean square error of GPS Height Fitting
FGUI工程打包发布&导入Unity&将UI显示出来的方式
[rtklib 2.4.3 B34] version update introduction I
What are the functions and features of helm or terrain
Idea problem record
KF UD decomposition pseudo code implementation advanced [2]
FairyGUI增益BUFF数值改变的显示
Mixed use of fairygui button dynamics
KF UD分解之UD分解基础篇【1】
[algorithm] sword finger offer2 golang interview question 2: binary addition
Music playback (toggle & playerprefs)
Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
WSL common commands
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)