簡體   English   中英

JSP中的out.println

[英]out.println in JSP

我寫了這個jsp代碼。 這是一個基本的登錄控件。 我正在嘗試檢查數據庫的輸入。 如果它們是正確的,那么沒有問題,但是如果輸入為null或它們不在數據庫中,我想給出錯誤信息並將訪客重定向到登錄頁面。

<%@ page import ="java.sql.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Login Process</title>
</head>
<body>
<%
    String userid = request.getParameter("username");    
    String pwd = request.getParameter("password");
    Class.forName("com.mysql.jdbc.Driver");

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost/ecommerce", "root1" , "1234");
    Statement st = con.createStatement();
    ResultSet rs;
    rs = st.executeQuery("select * from users where UserFirstName='" + userid + "' and UserPassword='" + pwd + "'");
    while(rs.next()){
        if(userid.length()!= 0 || pwd.length()!= 0){
            if (rs.getInt("Admin") == 1) {
                session.setAttribute("userid", userid);
                response.sendRedirect("Admin.jsp");
            } else if(rs.getInt("Admin") == 0){
                session.setAttribute("userid", userid);
                response.sendRedirect("User.jsp");
            }
            else {
                out.println("Invalid username or password <a href='index.jsp'>try again</a>");
            }
        }
        else{
            out.println("Empty username or password <a href='index.jsp'>try again</a>");
        }
    }
%>
</body>

在此代碼中兩行不起作用

else {
           out.println("Invalid username or password <a href='index.jsp'>try again</a>");
            }

還有這個

else{
            out.println("Empty username or password <a href='index.jsp'>try again</a>");
        }

我的瀏覽器只給我一個空白頁,而不是范圍。

如果輸入為空或它們不在數據庫中,我想給出錯誤

使用if (rs.next())代替while (rs.next())來檢查是否沒有記錄。


示例代碼:(根據您的要求進行修改)

String userid = request.getParameter("username");
String pwd = request.getParameter("password");

// check for empty username or password before making a call to database
if (userid == null || userid.trim().length() == 0 || pwd == null
        || pwd.trim().length() == 0) {
   out.println("Invalid username or password <a href='index.jsp'>try again</a>");
} else {
   ... // request for database query
   if (rs.next()) { // use if instead of while to check for no record
   ...
   } else {
     out.println("Empty username or password <a href='index.jsp'>try again</a>");
   }
}

將代碼放在try-catch塊中,如果發生任何異常,將重定向到登錄頁面。

try
{
String userid = request.getParameter("username");    
String pwd = request.getParameter("password");
Class.forName("com.mysql.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://localhost/ecommerce", "root1" , "1234");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select * from users where UserFirstName='" + userid + "' and UserPassword='" + pwd + "'");

while(rs.next())
{
    if(userid.length()!= 0 || pwd.length()!= 0){
    if (rs.getInt("Admin") == 1) {
            session.setAttribute("userid", userid);
            response.sendRedirect("Admin.jsp");
      } else if(rs.getInt("Admin") == 0){
            session.setAttribute("userid", userid);
            response.sendRedirect("User.jsp");
        }
        else {
            out.println("Invalid username or password <a href='index.jsp'>try again</a>");
        }
    }
    else{
        out.println("Empty username or password <a href='index.jsp'>try again</a>");
    }
}
}
  catch (Exception e)
    {
          out.println("Exception: "+e.getMessage()+" :Invalid username or password <a href='index.jsp'>try again</a>");
      }

e.getMessage()將顯示異常消息,它是可選的,因為它有助於調試,但如果在生產系統中,則可能會顯示敏感信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM