0

下面是一个用于从 jsp 页面获取参数的 servlet。

我正在尝试运行以下代码-

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.swing.JFrame;


public class oneServlet extends HttpServlet {

public static Connection getConnection() throws Exception {

    String driver = "org.postgresql.Driver";
    String url = "jdbc:postgresql://10.1.11.112:5432/pack";
    String username = "pack";
    String password = "pack";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
}

public static void main(String[] args) throws Exception {
    String user=request.getParameter("t1");
    String pass=request.getParameter("t2");
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        String queryTest = "select username,password from login";
        pstmt = conn.prepareStatement(queryTest);

        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
         String username=rs.getString(1);
         String password=rs.getString(2);    

   if(user.equals(username) && pass.equals(password))
       {

     JFrame frame = new JFrame("/LoginSuccess.jsp");

       }    
        else
        {

            System.out.println("Login Failed,Please try Againe");
        }
        }}

     catch (Exception e) {
        e.printStackTrace();
    } finally {
        pstmt.close();
        conn.close();
    }
}

}

它在 request.getParameter 中显示“请求无法解析”的错误。任何人都可以帮我解决这个问题。

4

2 回答 2

1

当你 extends HttpServlet,你需要覆盖doGetdoPost()which takeHttpServletRequestHttpServletResponse作为参数。

例子:

public void doPost(HttpServletRequest request, HttpServletResponse response)
           throws IOException, ServletException {
  .......
       String user=request.getParameter("t1"); //Use request variable to do get...
}

在这里这里阅读更多

于 2013-02-08T04:47:12.913 回答
1

Servlet 没有 main() ,它们由 SelvletContainer 或 Webserver 执行(如 tomcat)

在你的场景中。

public void doGet(){
    String user=request.getParameter("t1");
    String pass=request.getParameter("t2");
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        String queryTest = "select username,password from login";
        pstmt = conn.prepareStatement(queryTest);

        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
         String username=rs.getString(1);
         String password=rs.getString(2);    


       if(user.equals(username) && pass.equals(password))
           {
     //JFrame frame = new JFrame("/LoginSuccess.jsp");
     request.getRequestDispatcher().redirect("/LoginSuccess.jsp");

       }    
        else
        {

            System.out.println("Login Failed,Please try Againe");//This will print at the console
        }
        }}

     catch (Exception e) {
        e.printStackTrace();
    } finally {
        pstmt.close();
        conn.close();
    }

了解有关servlet的更多信息

于 2013-02-08T04:56:04.390 回答