Tomcat 部署项目后登陆成功无法跳转到主页面

记:

因为新做项目的时候,自己太浪,在tomcat配置的时候加了一个前缀

 

因为之前都是默认的,这个项目因为是想要添加一个前缀好将项目分开,避免项目太多不好分别(因为公司项目框架前端UI一样)

结果导致了项目启动后登陆后无法跳转到项目主页(一直报404错误),在后台请求地址找了没有报错(大大绿色),最后花费了1个小时左右,又是换tomcat版本,又是重新配置redis等等。最后请教了公司一个大牛(因为框架最初就是他搭的),最后说是不是回调地址是不是有问题...尴尬........

最终错误原因:因为在上面没有配置前缀,导致了项目虚拟路径错误,导致项目中回调地址错误。

好的,以下是一个简单的实现: 1. 创建数据库 创建一个名为news_system的数据库,并在其中创建一个名为user的表,包含三个字段:id、username、password。其中,id为自增键,username和password为varchar类型,长度为50。 2. 创建登录页面 在WebContent目录下创建login.jsp文件,代码如下: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录页面</title> </head> <body> <h1>欢迎登录新闻发布系统</h1> <form action="LoginServlet" method="post"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> <input type="submit" value="登录"> <input type="button" value="注册" onclick="location.href='register.jsp'"> </form> </body> </html> ``` 3. 创建注册页面 在WebContent目录下创建register.jsp文件,代码如下: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>注册页面</title> </head> <body> <h1>欢迎注册新闻发布系统</h1> <form action="RegisterServlet" method="post"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> <input type="submit" value="注册"> <input type="button" value="返回登录" onclick="location.href='login.jsp'"> </form> </body> </html> ``` 4. 创建Servlet 在src目录下创建LoginServlet.java文件,代码如下: ```java package com.example.servlet; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { // 加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 获取连接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/news_system", "root", "123456"); // 执行查询 stmt = conn.prepareStatement("SELECT * FROM user WHERE username=? AND password=?"); stmt.setString(1, username); stmt.setString(2, password); rs = stmt.executeQuery(); // 判断是否匹配成功 if (rs.next()) { // 匹配成功跳转页面 response.sendRedirect("index.jsp"); } else { // 匹配失败,返回登录页面 response.sendRedirect("login.jsp"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭资源 try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } ``` 在src目录下创建RegisterServlet.java文件,代码如下: ```java package com.example.servlet; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/RegisterServlet") public class RegisterServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); Connection conn = null; PreparedStatement stmt = null; try { // 加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 获取连接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/news_system", "root", "123456"); // 执行插入 stmt = conn.prepareStatement("INSERT INTO user (username, password) VALUES (?, ?)"); stmt.setString(1, username); stmt.setString(2, password); stmt.executeUpdate(); // 跳转至登录页面 response.sendRedirect("login.jsp"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭资源 try { if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } ``` 5. 配置Web.xml 在WebContent目录下创建WEB-INF目录,在其中创建web.xml文件,代码如下: ```xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>news_system</display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app> ``` 6. 启动Tomcat项目部署Tomcat服务器上,并启动Tomcat。在浏览器中输入http://localhost:8080/news_system/login.jsp即可访问登录页面
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值