猿类不苟且 2017-11-21 15:05 采纳率: 100%
浏览 7726
已采纳

使用java web 实现登录功能,servlet跳转jsp失败,页面出现空白,求大神解决!!!

package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import dbc.JdbcUtil;

public class UserDao {

public boolean register(String username,String userpw,String identity)throws Exception{

    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;


        conn = JdbcUtil.getConnection();
        String sql = "select * from user where username=? and userpw=? and identity=?";
        ps = conn.prepareStatement(sql);
        ps.setString(1,username);
        ps.setString(2, userpw);
        ps.setString(3, identity);
        rs = ps.executeQuery();
        if (rs.next()) return true;
        else return false;
        }

}

`package dbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JdbcUtil {
private static String driver;
private static String url;
private static String user;
private static String password;
private static Properties pr = new Properties();

private JdbcUtil() {
}

// ��Ƹù�����ľ�̬��ʼ�����еĴ��룬�ô�����װ����ʱִ�У���ִֻ��һ��
static {
    try {
        pr.load(JdbcUtil.class.getClassLoader().getResourceAsStream(
                "db.properties"));
        driver = pr.getProperty("driver");
        url = pr.getProperty("url");
        user = pr.getProperty("user");
        password = pr.getProperty("password");
        Class.forName(driver);
    } catch (Exception e) {
        throw new ExceptionInInitializerError(e);
    }
}

// ��ƻ�����Ӷ���ķ���getConnection()
public static Connection getConnection() throws SQLException {
    return DriverManager.getConnection(url, user, password);
}

// ����ͷŽ���������ӵķ���free()
public static void free(ResultSet rs, Statement st, Connection conn) {
    try {
        if (rs != null) {
            rs.close();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (st != null) {
                st.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {

            }
        }
    }
}

}

```package servlets;

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 dao.UserDao;
import vo.User;
import java.sql.*;
public class RegistServlet extends HttpServlet {

/**
 * 
 */
private static final long serialVersionUID = 1L;


/**
 * Constructor of the object.
 */
public RegistServlet() {
    super();
}

/**
 * Destruction of the servlet. <br>
 */
public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
}

/**
 * The doGet method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    doPost(request,response);
}

/**
 * The doPost method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String un=request.getParameter("username");
    String uw=request.getParameter("userpw");
    String iden=request.getParameter("identity");
    User uu=new User(un,uw,iden);
    UserDao c1 = null;
    PrintWriter out = null;

    try {
        if(c1.register(uu.getUsername(), uu.getUserpw(), uu.getIdentity()))
            request.getRequestDispatcher("/output.jsp").forward(request, response);
        else
            out.print("<script> alert(\"登录失败!\"); </script>");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }


        // TODO Auto-generated catch block



/**
 * Initialization of the servlet. <br>
 *
 * @throws ServletException if an error occurs
 */
public void init() throws ServletException {
    // Put your code here
}

}

```package vo;

public class User {
private String username;
private String userpw;
private String identity;
public User(String username, String userpw, String identity) {
this.identity=identity;
this.username=username;
this.userpw=userpw;
// TODO Auto-generated constructor stub
}
public String getUsername(){return username;}
public String getUserpw(){return userpw;}
public String getIdentity(){return identity;}
public void setUsername(String username){this.username=username;}
public void setUserpw(String userpw){this.userpw=userpw;}
public void setIdentity(String identity){this.identity=identity;}
}

url=jdbc:mysql://localhost:3306/LibSytem?useUnicode=true&characterEncoding=utf-8
user=root
password=960714

```<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head><title>登录页面</title> </head>
  <body>
<center><h1>图书馆用户登录</h1></center>
<center>
  <hr width="100%" size="1" color="black">
<form action="RegistServlet" method="post">
<table>
<tr><td>登录名:</td><td><input type="text" name="username"></td></tr>
<tr><td>登录密码:</td><td><input type="password" name="userpw"></td></tr>
<tr><td>身份:</td><td><select name="identity">
<option selected>用户</option>
<option>管理员</option>
</select></td></tr>
<tr><td><input type="submit" value="登录"></td></tr>

</table>
</form>
</center>
</body>
</html>





```<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>成功</title>

  </head>

  <body>
    This is my JSP page. <br>
  </body>
</html>


``














  • 写回答

3条回答 默认 最新

  • penguinDada 2017-11-22 02:16
    关注

    这种问题打几个断点调试一下就可以了,看你那个登录的jsp表单提交的action路径不对啊,配置的servlet确定是这样子的?应该连servlet都没请求到吧

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

报告相同问题?

悬赏问题

  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡
  • ¥20 基于MATLAB的TDOA
  • ¥15 为啥输入字体突然变了
  • ¥20 已知坐标,换成MATLAB可以用的数据
  • ¥15 这个python五子棋代码的每一句意思是什么啊
  • ¥15 求一段隐藏代码,隐藏一些内容