从jsp页面获取浏览器中的空值?

时间:2015-06-30 13:08:16

标签: java jsp jdbc

JSP页面

<%@ page import="Student.*"%>

<html>


<body>


      <%


   String id =request.getParameter("ID1");

     StudentDAO std=new StudentDAO();
    Student st=std.searchinfo("id");


   %>

        <h1> <%=st.showStudent()%> </h1>


 </body>

</html>

Student.java

import java.io.*;

public class Student implements Serializable  {

String name;

    String id1,phone,clas;

   public Student()
    {

    }

    public String showStudent()

    {

       return  "Name: "+name+" Address: "+clas+" Phone: "+phone+" ID: "+id1;
    }
   public void Student1(String  id,String n,String c,String ph)

    {

         name=n;
         id1=id;
         phone=ph;
        clas=c;
    }

}

StudentDAO.java

import java.sql.*; 

    import java.io.*;

     public class StudentDAO implements Serializable
    {

          static  String i,id,nam,clas,ph;

         Student studentinfo=new Student();

        public Student searchinfo(String id2)
        {
            id=id2;


        try{

        String url="jdbc:ucanaccess://C:\\Users\\Asim Iqbal\\Documents\\IT.accdb";

       Connection conn = DriverManager.getConnection(url);

        String sql= "SELECT * FROM Student WHERE ID=?";

                  PreparedStatement  stmt = conn.prepareStatement(sql);
                    stmt.setString(1,id);
                   ResultSet rs=stmt.executeQuery();
                    if (rs.next())
                    {
                        i=rs.getString("ID");

                      nam=rs.getString("Name"); 


                       clas=rs.getString("Class");
                        ph=rs.getString("Phone");
                       studentinfo.Student1(i, nam, clas, ph);
                    }
                } catch (SQLException e) {
                }
            return studentinfo;
       } 
    }

这是错误..但StudentDAO.java正确连接数据库并单独提供输出..但在浏览器中它给出如下的空值..

名称:null地址:null电话:null ID:null

1 个答案:

答案 0 :(得分:0)

<html>
<body>
      <%
   String id =request.getParameter("ID1");
     StudentDAO std=new StudentDAO();
  

学生st = std.searchinfo(id);

   %>
        <h1> <%=st.showStudent()%> </h1>
 </body>
</html>

如上所述更改jsp页面代码,您应该将id值作为参数传递给DAO方法,现在传递的是字符串&#34; id&#34;。

  

记得在jsp中编写java代码是一种不好的做法