[英]out.println in JSP
I wrote this jsp code. 我写了这个jsp代码。 It is a basic login control.
这是一个基本的登录控件。 I am trying to check inputs with my database.
我正在尝试检查数据库的输入。 If they are correct then there is no problem but if the inputs are null or they are not in database I want to give error and redirect the guest to the login page.
如果它们是正确的,那么没有问题,但是如果输入为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>
In this code two line is not working 在此代码中两行不起作用
else {
out.println("Invalid username or password <a href='index.jsp'>try again</a>");
}
and this one 还有这个
else{
out.println("Empty username or password <a href='index.jsp'>try again</a>");
}
my browser just gives me a blank page instead else scopes. 我的浏览器只给我一个空白页,而不是范围。
if the inputs are null or they are not in database I want to give error 如果输入为空或它们不在数据库中,我想给出错误
use if (rs.next())
instead of while (rs.next())
to check for no records. 使用
if (rs.next())
代替while (rs.next())
来检查是否没有记录。
Sample code: (Modify it as per your requirement) 示例代码:(根据您的要求进行修改)
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>");
}
}
Enclose the code in try - catch block and if any exception occurred will redirect to login page. 将代码放在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(), will show the exception message, is optional since it will help for debugging but may reveal sensitive information if it is in a production system. e.getMessage()将显示异常消息,它是可选的,因为它有助于调试,但如果在生产系统中,则可能会显示敏感信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.