Laughing girl2021
2021-06-01 16:56
采纳率: 100%
浏览 193

运行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条回答 默认 最新

相关推荐 更多相似问题