语句stmt1=con.createStatement(); 结果集rs1=stmt1.executeQuery(查询1); 字符串n,s; requestr=null; int i,世界其他地区; while(rs1.next()) { rs1.last(); row=rs1.getRow(); rs1.first(); i=rs1.getInt(“Id”); 请求设置属性(“Id”,i); n=rs1.getString(“名称”); 请求setAttribute(“名称”,n); s=rs1.getString(“技能”); 要求设置属性(“技能”,s); 请求setAttribute(“行”,行); req.getRequestDispatcher(“/Jsp/LoggedIn.Jsp”).forward(req,res); } } 其他的 { out.println(“+”用户名或密码不匹配!”+); out.println(“”); } } 捕获(例外e) { e、 printStackTrace(); } } }
2) Login.jsp语句stmt1=con.createStatement(); 结果集rs1=stmt1.executeQuery(查询1); 字符串n,s; requestr=null; int i,世界其他地区; while(rs1.next()) { rs1.last(); row=rs1.getRow(); rs1.first(); i=rs1.getInt(“Id”); 请求设置属性(“Id”,i); n=rs1.getString(“名称”); 请求setAttribute(“名称”,n); s=rs1.getString(“技能”); 要求设置属性(“技能”,s); 请求setAttribute(“行”,行); req.getRequestDispatcher(“/Jsp/LoggedIn.Jsp”).forward(req,res); } } 其他的 { out.println(“+”用户名或密码不匹配!”+); out.println(“”); } } 捕获(例外e) { e、 printStackTrace(); } } },jsp,servlets,jdbc,Jsp,Servlets,Jdbc,2) Login.jsp 登录页面 登录 用户名: 密码: 3) LoggedIn.jsp <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/htm
登录页面
登录
用户名:
密码:
3) LoggedIn.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>LoggedIn</title>
</head>
<body>
<h2>Welcome <%String s=request.getAttribute("Name").toString();
out.println(s); %></h2>
<table border="5" bordercolor="brown" background="yellow">
<tr>
<th>Id</th><th>Name</th><th>Skill</th>
</tr>
<!-- It is just for normal fetching values -->
<tr><td>
<% //by using setAttri and getAttru
//String s=request.getAttribute("Name").toString();
out.println(s);
//by using HttpSession
/* HttpSession sess=request.getSession(false);
String s=sess.getAttribute("Name").toString();
out.println(s);
*/
%></td></tr>
<%--This is for fetching the complete database and creating rows dynamically --%>
<%
int rows=Integer.parseInt(request.getAttribute("Rows").toString());
for(int j=1;j<=rows;j++ )
{
%>
<tr>
<td>
<%
out.println(s);
%>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
ArrayList<Student> al = new ArrayList<Student>();
while(rs1.next())
{
Student std= new Student();
std.setName(n);
...
al.add(std);
}
request.setAttribute("userList" al);
洛格丁
欢迎
IdNameSkill
使用PreparedStatement而不是登录语句来防止SQL注入
rs1.last();
row=rs1.getRow();
rs1.first()
上面的代码导致一个无限循环
在循环期间将调度程序置于外部
你为什么用这个
row=rs1.getRow();
您可以使用ArrayList/LinkedHashmap存储所有记录,然后转发到LoggedIn.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>LoggedIn</title>
</head>
<body>
<h2>Welcome <%String s=request.getAttribute("Name").toString();
out.println(s); %></h2>
<table border="5" bordercolor="brown" background="yellow">
<tr>
<th>Id</th><th>Name</th><th>Skill</th>
</tr>
<!-- It is just for normal fetching values -->
<tr><td>
<% //by using setAttri and getAttru
//String s=request.getAttribute("Name").toString();
out.println(s);
//by using HttpSession
/* HttpSession sess=request.getSession(false);
String s=sess.getAttribute("Name").toString();
out.println(s);
*/
%></td></tr>
<%--This is for fetching the complete database and creating rows dynamically --%>
<%
int rows=Integer.parseInt(request.getAttribute("Rows").toString());
for(int j=1;j<=rows;j++ )
{
%>
<tr>
<td>
<%
out.println(s);
%>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
ArrayList<Student> al = new ArrayList<Student>();
while(rs1.next())
{
Student std= new Student();
std.setName(n);
...
al.add(std);
}
request.setAttribute("userList" al);
ArrayList al=new ArrayList();
while(rs1.next())
{
Student std=新生();
标准设置名称(n);
...
新增(标准);
}
setAttribute(“userList”al);
对于第二个问题,我建议使用。我不能说明问题1的原因,但您可以使用response.sendRedirect(“/Jsp/LoggedIn.Jsp”)在成功登录后重定向到此页面in@ABV尝试使用返回键statement@hd1但是使用它…我可以从servlet一个接一个地将值传递给JSP?@JqueryLearner我只是不想重定向到另一个页面,但也要将req和res对象传递给process,以访问JSPsry中的值,但这里的Student是什么:ArrayList al=new ArrayList();这样,您就可以在该类的对象中设置多个属性(一个学生信息),并将该对象添加到ArrayListi,意思是学生是一个类??因为在我的程序中,我没有学生课。如果你需要将多个记录(行)发送到另一个页面,那么你需要ArrayList,并且需要学生课
ArrayList<Student> al = new ArrayList<Student>();
while(rs1.next())
{
Student std= new Student();
std.setName(n);
...
al.add(std);
}
request.setAttribute("userList" al);