Laughing girl2021 2021-06-01 16:56 采纳率: 100%
浏览 348
已结题

运行javaweb项目时,不能显示页面,本人萌新,求大神解答!


package com.pack.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pojo.ject.ject;
import com.userservlet.userservlet;

public class servlet2 extends HttpServlet {
    userservlet us=new userservlet();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("GBK");
        response.setCharacterEncoding("GBK");
        String uname=request.getParameter("uname");
        String upwd=request.getParameter("upwd");
        PrintWriter pw=response.getWriter();            
        if(uname!=null && upwd!=null) {
            com.pojo.service.service u=us.login(uname, upwd);
            if(u!=null) {
                pw.print("恭喜你!"+u.getUsername()+"登录成功!");
            }else {
                pw.print("登录失败!你输入的用户名或密码错误!");
            }
        }else {
            pw.print("你输入的用户名或密码为空!");
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        
        doGet(request, response);
    }

}

 

//实体类
package com.pojo.service;

public class service {
    
    private int id;
    private String username;
    private String password;    
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    

}

//数据库操作
package com.pojo.serlaive;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.pojo.ject.ject;
import com.pojo.service.service;

public class serlaive {
    //定义一个动态数组链表类用来存放数据库中的数据
    public ArrayList findUserBySql(String sql) throws SQLException {
        //创建一个空的数据链表用来存放指定对象数据
        ArrayList<service> userList=new ArrayList<service>();
        //导入数据库连接对象与数据库连接
        Connection conn=ject.getConn();
        Statement stmt=conn.createStatement();
        System.out.println("数据库查询中····");
        System.out.println("SQL语句:"+sql);
        ResultSet rs=stmt.executeQuery(sql);
        while(rs.next()) {
            service u=new service();
            u.setId(rs.getInt("id"));
            u.setUsername(rs.getString("username"));
            u.setPassword(rs.getString("password"));
            userList.add(u);
        }
        conn.close();
        stmt.close();
        return userList;        
    }

}
 

//具体业务方法
package com.userservlet;

import java.sql.SQLException;
import java.util.ArrayList;

import com.pojo.serlaive.serlaive;
import com.pojo.service.service;

public class userservlet {
    //创建数据库操作类的变量ud
    serlaive ud=new serlaive();
    
    public service login(String name,String pwd) {
        service u=null;
        String sql="select*from user where username='"+name+"' and password='"+pwd+"'";
        try {
            ArrayList<service> userList=ud.findUserBySql(sql);
            //如果userList不为空
            if(userList.size()>0) {
                //等于userList的第一个元素
                u=userList.get(0);
            }
        } catch (SQLException e) {            
            e.printStackTrace();
        }
        return u;
    }

}

//数据库连接
package com.pojo.ject;

import java.sql.Connection;
import java.sql.DriverManager;

public class ject {
    
    private static String address="jdbc:mysql://localhost:3306/text1?useSSL=false&serverTimeZone=UTC";
    private static String dataBaseName="****";          
    private static String dataBasePwd="**********";

    public static Connection getConn(){
        Connection conn=null;        
            try{
                //加载jdbc驱动,固定程式
                Class.forName("com.mysql.cj.jdbc.Driver");
                //链接数据库,固定程式
                conn=DriverManager.getConnection(address,dataBaseName,dataBasePwd);
            }catch(Exception e){
                e.printStackTrace();
            System.out.println("链接失败!");
            }        
        return conn;
    }

}

 

//html内容

<!DOCTYPE html>
<html>
<head>
<meta charset="GBK">
<title>用户登录</title>
</head>
<body>
   <form action="http://127.0.0.1:8080/unit4/servlet2" method="post">
                     用户名:<input type="text" name="uname">
                     密码:    <input type="password" name="upwd">
           <input type="submit">
   </form>
</body>
</html>

 

//核心组件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>unit4</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>servlet2</servlet-name>
    <servlet-class>com.pack.servlet.servlet2</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>servlet2</servlet-name>
    <url-pattern>/servlet2</url-pattern>
  </servlet-mapping>
</web-app>

最后输入http://127.0.0.1:8080/unit4/login1.html这个网址后,就变成上图情况,求大神解答?

这个是项目结构

  • 写回答

6条回答 默认 最新

  • 关注

    路径写错了,看看控制台的报错信息。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 8月20日
  • 已采纳回答 8月12日

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。