dhhjj 2014-04-11 02:10 采纳率: 0%
浏览 6311

基于MyEclipse的登录界面的设计

在MyEclipse下创建servlet,代码如下:
package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

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

import java.sql.*;

public class LoginServlet extends HttpServlet {

private String name;
private String pass;

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

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


/**
 * 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 {

    this.name=request.getParameter("account");
    this.pass=request.getParameter("password");
       PrintWriter out=response.getWriter();
       //驱动程序名
       String driverName="oracle.jdbc.driver.oracleDriver";
       //数据库用户名
       String userName="system";
       //密码
       String userPasswd="zmh";
       //数据库名
       String dbName="XE";
       //表名
       String tableName="lib_user";
       //联结字符串
       String url="jdbc:oracle:thin:@localhost:1512:XE";
      try
      {
       Class.forName("oracle.jdbc.driver.oracleDriver");
       Connection connection=DriverManager.getConnection(url);
       Statement statement = connection.createStatement();
       String sql="SELECT * FROM +lib_user";
       ResultSet rs = statement.executeQuery(sql);
        while(true) 
          {
              if(rs.next())
              {
                   if((this.name.equals(rs.getString(1)))&&(this.pass.equals(rs.getString(2))))
                   {

                      out.print("<h1>"+"Login Success!");

                      break;
                    }
              }
              else
              {
                 out.print("<h1>"+"Account or Password Is Invalid!");
                 break;
              }
          }         
          return;
        }
       catch(SQLException e)
       {
       }
       catch(ClassNotFoundException e)
       {
       }
}

/**
 * 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);
}


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

}
创建login.jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>

function login_click() { form1.action="Login"; form1.submit(); }
Account:
Password:



配置web.xml:
<?xml version="1.0" encoding="UTF-8"?>

LibraryService


A Login Servlet
LoginServlet
LoginServlet
servlets.LoginServlet


LoginServlet
/LoginServlet


login.jsp


在浏览器输入:http://localhost:8088/LibraryService/login.jsp能显示登录界面,但是输入账号和密码后显示如下:
HTtype Status report

message /LibraryService/Login

description The requested resource is not available.

Apache Tomcat/7.0.53TP Status 404 - /LibraryService/Login
求高手指教,本人刚接触这类软件,实在是找不出问题来,求指教~~~~~~~~~~~~

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-01 22:43
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    您的代码看起来是正确的,因为您已经成功地使用Java EE(特别是Spring MVC)框架实现了一个简单的登录功能。然而,当用户尝试进行登录时,他们似乎遇到了一些问题。

    首先,要解决这个问题,您可以检查以下几点:

    1. 确保您的数据库连接正确。
    2. 确保您的SQL查询语句(SELECT * FROM lib_user)是有效的,并且返回了所需的数据列。
    3. 确保您的JDBC驱动程序和数据库名称、用户名、密码等信息都是正确的。
    4. 检查用户的输入是否正确(例如,确保它们符合预期格式)。
    5. 您可能还需要考虑对用户输入的异常处理,以防止潜在的安全漏洞。

    此外,如果您正在使用Tomcat作为服务器,则可以考虑设置适当的错误页面或配置文件来捕获特定类型的错误并提供友好的反馈给用户。这可以通过修改web.xml文件中的错误处理配置来实现。

    综上所述,为了更有效地解决问题,请仔细检查您的代码和数据库连接、SQL查询以及用户输入。如果仍然无法解决问题,请提供更多关于您的环境细节和具体错误消息的信息。

    评论

报告相同问题?