【问题标题】:How should I create this new page? JSP or servlet?我应该如何创建这个新页面? JSP 还是 servlet?
【发布时间】:2016-01-01 09:34:05
【问题描述】:

我对 Web 开发完全陌生,我需要一些帮助。我正在使用 Java Eclipse EE、tomcat 服务器和 mysql 做一个工资系统 web 应用程序项目。我使用了一个教程并设法在下面创建了登录界面。所以现在,当我单击输入我的登录详细信息并单击登录(在 localhost:8080/Payroll)时,我希望它转到一个网页(我不知道如何创建)并显示按钮列表(任何随机我以后可以重命名的按钮)。有人可以帮帮我吗。我不知道如何使用 .JSP、.html、.java,而且我对这些文件类型如何帮助我获得我想要的东西感到非常困惑。请帮助某人,我只想将登录按钮重定向到带有按钮的网页。谢谢。

Login.java (Servlet)

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class Login extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    String employee_id = request.getParameter("employee_id");
    String password = request.getParameter("password");
    if(Validate.checkUser(employee_id, password)) { 
        RequestDispatcher rs = request.getRequestDispatcher("**SOME FILE NAME HERE TO REDIRECT TO?**");
        rs.forward(request, response);
    }
    else
    {
       out.println("Employee ID or Password is incorrect. Please try again.");
       RequestDispatcher rs = request.getRequestDispatcher("index.html");
       rs.include(request, response);
    }
}  


    }

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<form action="login" method="post">
<h3>
Employee Login
</h3>
 <b>Employee ID:</b> <br>
 <input type="text"name="employee_id" size="20"><br><br>
<b>Password:</b><br>
<input type="password" name="password" size="20"><br><br>
<input type="submit" value="Login"><br><br>
</form>
</body>
</html>

Validate.java(类文件)

import java.sql.*;
public class Validate
{
    public static boolean checkUser(String employee_id, String password)
    {
        boolean st = false;
        try { 
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/payroll_system", "root", ""); 
            PreparedStatement ps = con.prepareStatement("select * from employee_login where employeeID = ? and pwd = ?");
            ps.setString(1, employee_id);
            ps.setString(2, password);
            ResultSet rs =ps.executeQuery();
             st = rs.next();

         }catch(Exception e)
          {
              e.printStackTrace();
          }
             return st;                 
      }   
    }

【问题讨论】:

    标签: java mysql jsp tomcat servlets


    【解决方案1】:

    由于您的 index.html 已经是一个“带有按钮的页面”,这似乎对您有用。

    但是,如果您的页面将包含动态数据,那么您最好使用 jsp。

    您可能希望首先将用户名设置到会话中,然后在 jsp 中显示:

        request.setAttribute("user", employee_id);
        RequestDispatcher rs = request.getRequestDispatcher("stackoverflow.jsp");
        rs.forward(request, response);
    

    在stackoverflow.jsp中

      <h2><%= request.getAttribute("user") %></h2>
    

    现在比较一下直接调用jsp和通过登录调用时...

    请务必阅读 Web 项目中的 MVC,并在对这些概念有所了解后考虑使用框架(如 Spring)。

    关于您的代码的一些注释:

    您永远不会关闭 Connection、Statement、ResultSet。考虑一个连接池和 try-with-resource:

    public static boolean checkUser(String employee_id, String password)
    {
        boolean st = false;
        try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/payroll_system", "root", ""); 
               PreparedStatement ps = 
                    con.prepareStatement("select * from employee_login where employeeID = ? and pwd = ?")) {
            ps.setString(1, employee_id);
            ps.setString(2, password);
            try(ResultSet rs =ps.executeQuery()) {
              return rs.next();
            }
         }catch(Exception e) {
              e.printStackTrace();
          }
         return false;                 
      }   
    

    【讨论】:

    • "您可能希望首先将用户名设置到会话中,然后在 jsp 中显示。"你能帮我解决这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 2019-01-14
    • 2015-02-10
    • 1970-01-01
    相关资源
    最近更新 更多