当我尝试从Java类获取值到JSP时,我的JSP显示“nul”

时间:2013-06-30 08:38:49

标签: java jsp servlets

我是Java的学习者,请帮助我。这是我的示例代码,当我执行此程序时。它应该从数据库中检索信息并将值存储在Java类中,然后将值发送到JSP,但当我查看JSP时,它显示值“null”

以下是与我的问题相关的5个文件,我们很乐意接受任何答案或解决方案。

-------------------------这是我的基本输入表格-------------- -------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WELCOME TO DISCOUNTLOVERS.IN</title>



<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
</head>

<body>

<div id="wrapper">
<div><%@include file="header.jsp" %></div>

<div><table width="920" border="0" cellpadding="0" cellspacing="0" id="main-content" > 

<form method ="post" action ="Registrationform" >
<tbody>
<tr>
<td width="382" valign="top" class="formtext">
<div>First Name</div>
<div>Last name</div>
<div>E-mail</div>
<div>Password</div>
<div>Confirm Password</div>
<div>phone no</div>
<div>Address</div>
<div>City</div>
<div>State</div>
<div>country</div>
<div>post code</div>
</td>
<td width="52" class="formtext-gap">
<div>:</div>
<div>:</div>
<div>:</div>
<div>:</div>
<div>:</div>
<div>:</div>
<div>:</div>
<div>:</div>
<div>:</div>
<div>:</div>
<div>:</div>
</td>
<td width="486" valign="top">
<div><input type="text" name="username_first"/></div>
<div><input type="text" name="username_last"/></div>
<div><input type="text" name="username_email"/></div>
<div><input type="text" name="username_pw"/></div>
<div><input type="text" name="username_cpw"/></div>
<div><input type="text" name="username_tele"/></div>
<div><input type="text" name="username_add"/></div>
<div><input type="text" name="username_city"/></div>
<div><input type="text" name="username_state"/></div>
<div><input type="text" name="username_country"/></div>
<div><input type="text" name="username_postcode"/></div>
</td>
</tr>
<tr><td colspan="3" align="center"><input type="checkbox"/> Terms and conditions of Discountlovers.in accpeted</td></tr>
<tr><td colspan="3" align="center"><div><input type="submit" value="Submit" class=""/></div></td></tr>
    </tbody></form></table></div>
<div><%@include file="footer.jsp" %></div>
</div>

</body>
</html>

------------------------这是数据插入servlet --------------- ---------

package registration_Servlets;


import registration_Servlets.intialization_form;
import java.io.IOException;
import java.io.PrintWriter;

import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class Registrationform
 */
@WebServlet("/Registrationform")
public class Registrationform extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Registrationform() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");
          PrintWriter pw = response.getWriter();
        intialization_form obj = new intialization_form();
        String name;
        String lastname;
        String username_email;
        String username_pw ;
        String username_cpw;
        String id ;
        String username_add;
        String username_city ;
        String username_state;
        String username_country;
        String username_postcode;
        try {
            name  = request.getParameter("username_first");
            lastname = request.getParameter("username_last");
            username_email = request.getParameter("username_email");
            username_pw = request.getParameter("username_pw");
            username_cpw = request.getParameter("username_cpw");
            id = request.getParameter("username_tele");
            username_add = request.getParameter("username_add");
            username_city = request.getParameter("username_city");
            username_state = request.getParameter("username_state");
            username_country = request.getParameter("username_country");
            username_postcode = request.getParameter("username_postcode");
            DataBase db = new DataBase();
            PreparedStatement pst = db.getConnection().prepareStatement("insert into registrationinfo values(?,?,?,?,?,?,?,?,?,?,?)"); 
             pst.setString(1, name );
             pst.setString(2, lastname);
             pst.setString(3, username_email);
             pst.setString(4, username_pw);
             pst.setString(5, username_cpw); 
             pst.setString(6, id);
             pst.setString(7, username_add);
             pst.setString(8, username_city);
             pst.setString(9, username_state);
             pst.setString(10, username_country);
             pst.setString(11, username_postcode);
             int i = pst.executeUpdate();
             if(i==1){
                 //request.setAttribute("name",name);
                 /*String site = new String("Retrivin_user_reg_form");

                  response.setStatus(response.SC_MOVED_TEMPORARILY);
                  response.setHeader("Location", site); */
                 response.sendRedirect("Retrivin_user_reg_form");
             }
            pw.println("done");

    }
        catch (Exception e) {
             e.printStackTrace();
        }
}
    }

--------------------------------这是我的数据检索servlet ------ -----------

package registration_Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Retrivin_user_reg_form
 */
@WebServlet("/Retrivin_user_reg_form")
public class Retrivin_user_reg_form extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Retrivin_user_reg_form() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
          PrintWriter pw = response.getWriter();

        try {

            DataBase db = new DataBase();
            PreparedStatement pst = db.getConnection().prepareStatement("Select * from registrationinfo"); 
            intialization_form obj = new intialization_form();
             ResultSet i = pst.executeQuery();
             while(i.next()){
            // pw.println("EmpName" + " " + "EmpSalary" + "<br>");
             obj.name= i.getString(1);
             obj.lastname=i.getString(2);
             obj.username_email=i.getString(3);
             obj.username_pw=i.getString(4);
             obj.username_cpw=i.getString(5);
             obj.id=i.getString(6);
             obj.username_add=i.getString(7);
             obj.username_city=i.getString(8);
             obj.username_state=i.getString(9);
             obj.username_country=i.getString(10);
             obj.username_postcode=i.getString(11);
             String site = new String("registrationcompleted.jsp");

              response.setStatus(response.SC_MOVED_TEMPORARILY);
              response.setHeader("Location", site);
             /*request.getRequestDispatcher("registrationcompleted.jsp").forward(request, response);*/
             }
    }
        catch (Exception e) {
             e.printStackTrace();
        }

    }
      protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        }

}

-----------这是我的Java类,我想存储我检索到的数据--------------

package registration_Servlets;

public class intialization_form
{
    String name;
    String lastname;
    String username_email;
     String username_pw;
     String username_cpw;
    String id;
    String username_add;
    String username_city;
    String username_state;
    String username_country;
    String username_postcode;

     public void setname( String value )
        {
         name = value;
        }
     public String getname() { return name; }

     public void setlastname( String value )
        {
         lastname = value;
        }
     public String getlastname() { return lastname; }

     public void setusername_email( String value )
        {
         username_email = value;
        }
     public String getusername_email() { return username_email; }

     public void setusername_pw(String value){
         username_pw= value;
     }
     public String getusername_pw() { return username_pw; }

     public void setusername_cpw(String value){
         username_cpw=value;
     }
     public String getusername_cpw(){
         return username_cpw;
     }
     public void setid(String value){
         id= value;
     }
     public String getid(){
         return id;
     }
     public void setusername_add(String value){
         username_add= value;
     }
     public String getusername_add(){
         return username_add;
     }
     public void setusername_city(String value){
         username_city= value;
     }
     public String getusername_city(){
         return username_city;
     }
     public void setusername_state(String value){
         username_state= value;
     }
     public String getusername_state(){
         return username_state;
     }
     public void setusername_country(String value){
         username_country = value;
     }
     public String getusername_country(){
         return username_country;
     }
     public void setusername_postcode(String value){
         username_postcode= value;
     }
     public String getusername_postcode(){
         return username_postcode;
     }


}

-------------------------这是我的输出JSP,我得到空值-------- -----

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
  <%@ page import = "registration_Servlets.intialization_form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WELCOME TO DISCOUNTLOVERS.IN</title>



<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrapper">
<div><%@include file="header.jsp" %></div>

<div><table width="920" border="0" cellpadding="0" cellspacing="0" id="main-content">
<tbody>
<tr>
<td>
<% intialization_form user = new intialization_form(); %>
first Name: <%= user.getname() %> 
last name: <%= user.getlastname() %><BR>
Email: <%= user.getusername_email() %><BR>
</td>
</tr>
    </tbody></table></div>
<div><%@include file="footer.jsp" %></div>
</div>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

这是因为你表单类中的值是NULL。您永远不会为它们分配任何值。

您只需创建一个空表单对象并“打印”其中的一些值。

<% intialization_form user = new intialization_form(); %>
first Name: <%= user.getname() %> 
last name: <%= user.getlastname() %><BR>
Email: <%= user.getusername_email() %><BR>

@See https://stackoverflow.com/tags/servlets/info获取有关如何启动jsp和servlet编程的一些信息。

答案 1 :(得分:0)

我可以看到你使用过

request.getRequestDispatcher("registrationcompleted.jsp").forward(request, response);

在执行此forward()方法

之前的下面给出的行之前
request.setAttribute("your-key", yourObjectReference);

request对象的此方法会将your form object设置为您要在output jsp中访问的属性,您可以使用request.getAttribute("key")方法检索该属性。

将以下代码放在输出jsp中。

<% intialization_form user = (intialization_form)request.getAttribute("your-key"); %>

由于request.getAttribute()方法返回Object,您必须将其投放到intialization_form

在执行此操作之前,请不要忘记从代码中删除以下行

response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", site);