这是我先写的jsp:two.jsp
<% String message= (String)session.getAttribute("message");
session.removeAttribute("message");
if (message!=null){
out.print(message);
}
%>
<form action="TwoServlet" method="post">
学生姓名:<input type="text" name="username"/> </br>
学生学号:<input type="password" name="password"/> </br>
<input type="submit" value=" 查询"/>
</form>
然后这是我的servlet:TwoServlet
public class TwoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username=request.getParameter("username");
String password=request.getParameter("password");
TwoJdbc jdbc = new TwoJdbc();
int i= jdbc.selectuser(username, password);
String url = i==1?"xueshengxinxi.jsp":"two.jsp";
if (url.equals("two.jsp")){
request.getSession().setAttribute("message","<font color='red'><b>该学生生不存在,或者学生姓名或密码输入错误,请重新输入学生姓名或者学生学号</b></font>");
}
request.setAttribute("us", username);
request.setAttribute("ps", password);
request.getRequestDispatcher(url).forward(request, response);
System.out.print(i);
System.out.print("---------------------");
}
接着是我的jdbc:TwoJdbc
public class TwoJdbc {
public int selectuser(String username,String password){
int i=0;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orac","test","test");//连接池
connection.setAutoCommit(false);
PreparedStatement ps=connection.prepareStatement("select count(*) from student where username=? and password=?");
ps.setString(1,username);
ps.setString(2,password);
ResultSet rs=ps.executeQuery();
rs.next();
i=rs.getInt(1);
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block&
e.printStackTrace();
}
return i;
}
最后是我的一个小jsp:xueshengxinxi.jsp
<%= request.getAttribute("us")%>
<%= request.getAttribute("ps") %>我的student表里面,有,username,password和Sushehao,3个字段,
我在two.jsp里面,只输入学生的username和password,然后能在xueshengxinxi.jsp里面能拿到
该生的姓名和密码!!
问题就是,我该怎么写代码才能在只输入username和password的情况下,能在xueshegnxinxi.jsp里面拿到表的第三个字段Sushehao!!
java初学者,谢谢各位帮忙!!
<% String message= (String)session.getAttribute("message");
session.removeAttribute("message");
if (message!=null){
out.print(message);
}
%>
<form action="TwoServlet" method="post">
学生姓名:<input type="text" name="username"/> </br>
学生学号:<input type="password" name="password"/> </br>
<input type="submit" value=" 查询"/>
</form>
然后这是我的servlet:TwoServlet
public class TwoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username=request.getParameter("username");
String password=request.getParameter("password");
TwoJdbc jdbc = new TwoJdbc();
int i= jdbc.selectuser(username, password);
String url = i==1?"xueshengxinxi.jsp":"two.jsp";
if (url.equals("two.jsp")){
request.getSession().setAttribute("message","<font color='red'><b>该学生生不存在,或者学生姓名或密码输入错误,请重新输入学生姓名或者学生学号</b></font>");
}
request.setAttribute("us", username);
request.setAttribute("ps", password);
request.getRequestDispatcher(url).forward(request, response);
System.out.print(i);
System.out.print("---------------------");
}
接着是我的jdbc:TwoJdbc
public class TwoJdbc {
public int selectuser(String username,String password){
int i=0;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orac","test","test");//连接池
connection.setAutoCommit(false);
PreparedStatement ps=connection.prepareStatement("select count(*) from student where username=? and password=?");
ps.setString(1,username);
ps.setString(2,password);
ResultSet rs=ps.executeQuery();
rs.next();
i=rs.getInt(1);
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block&
e.printStackTrace();
}
return i;
}
最后是我的一个小jsp:xueshengxinxi.jsp
<%= request.getAttribute("us")%>
<%= request.getAttribute("ps") %>我的student表里面,有,username,password和Sushehao,3个字段,
我在two.jsp里面,只输入学生的username和password,然后能在xueshengxinxi.jsp里面能拿到
该生的姓名和密码!!
问题就是,我该怎么写代码才能在只输入username和password的情况下,能在xueshegnxinxi.jsp里面拿到表的第三个字段Sushehao!!
java初学者,谢谢各位帮忙!!
private String username;
private String password;
private String Sushehao;
}
多加这么一个类。得到数据的时候new 一个实例,将从数据库得到的数据封装给这个类,然后再页面直接用这个类的实例打点调方法得到相映的属性,建议楼主可以看看JAVA OOP 面相对象的思想。
public List<Student> selectuser(String username,String password){
int i=0;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orac","test","test");//连接池
connection.setAutoCommit(false);
PreparedStatement ps=connection.prepareStatement("select count(*) from student where username=? and password=?");
ps.setString(1,username);
ps.setString(2,password); ResultSet rs=ps.executeQuery();
List<Student> students = new ArrayList<Student>();
while(rs.next()){
Student student = new Student();
student.setUserName(rs.getString(1));
student.setPassword(rs.getString(2));
student.setSushehao(rs.getInt(3));
students.add(student);
} }catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block&
e.printStackTrace();
}
return students;
}
rs.next();
i=rs.getInt(3);