将变量从jsp传递给servlet时获取null值

时间:2015-02-18 07:11:55

标签: java jsp servlets

我试图从jsp获取一个值到sevlet.First变量值由session setArribute(),getAttribute()获取然后我需要将该特定变量从jsp传递给servlet但是我得到的值为null变量

下面我将分享我的代码,任何人都可以知道如何解决这个问题?

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="java.sql.*"%>
<html>
<head>
    <title>Welcome to survey</title>
</head>
<body>
<%
    ResultSet rset;
    String sur_id = request.getParameter("surveyid");
    session.setAttribute( "surveyid", sur_id );
    int new_survey_id = Integer.parseInt(sur_id);

    if (request.getParameter("surveyid") == null) {
        out.println("Please enter your name.");
    } else {
        out.println("Hello <b>"+request.getParameter("surveyid")+"</b>!");
    }
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/surveysample", "root", "root");
    String query = "select * from surveydetail where surveyid ="+ new_survey_id ;
    Statement stmt = con.createStatement();
    rset = stmt.executeQuery(query);

    while(rset.next()){
//        out.println(rset.getString(1));
//        out.println(rset.getString(2));
//        out.println(rset.getString(3));
//        out.println(rset.getString(4));
        %>
        <table border="3">
            <tr><td>Survey_Id</td><td><%=rset.getString(1)%></td></tr>
            <tr><td>Family name</td><td><%=rset.getString(2)%></td></tr>
            <tr><td>First name</td><td><%=rset.getString(3)%></td></tr>
            <tr><td>Middle name</td><td><%=rset.getString(4)%></td></tr>
            <tr><td>gender</td><td><%=rset.getString(5)%></td></tr>
            <tr><td>dat of birth</td><td><%=rset.getString(6)%></td></tr>
            <tr><td>income</td><td><%=rset.getString(7)%></td></tr>
            <tr><td>complete address</td><td><%=rset.getString(8)%></td></tr>
            <tr><td>coordinates</td><td><%=rset.getString(9)%></td></tr>
            <tr><td>mobile number</td><td><%=rset.getString(10)%></td></tr>
            <tr><td>email address</td><td><%=rset.getString(11)%></td></tr>
            <tr><td>present Internet provider</td><td><%=rset.getString(12)%></td></tr>
            <tr><td>comments</td><td><%=rset.getString(13)%></td></tr>
            <tr><td>remarks</td><td><%=rset.getString(14)%></td></tr>
            <br>
            <form>
                <table>
                    <tr><td><input type="button" value="edit" onclick="javascript:document.forms[0].action = 'EditSurvey.jsp'; document.forms[0].submit();"></td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                        <td><input type="button" value="delete"></td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                        <td><input type="button" value="print"></td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                        <td><input type="button" value="send mail"></td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                    </tr>
                </table>
            </form>


        </table>
        <%
    }

%>



</body>
</html>

从上面的代码中我将会话变量设置为surveyid 我可以从下面的代码中得到它。它正常工作

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Edit survey</title>
</head>
<body>
<%
    ResultSet resultSet;
    String surveyId = (String) session.getAttribute("surveyid");
    out.println("check"+surveyId);

    request.setAttribute("surveyid", surveyId);


    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/surveysample", "root", "root");
    String query = "select * from surveydetail where surveyid ="+ surveyId ;
    Statement stmt = con.createStatement();
    ResultSet rset = stmt.executeQuery(query);

    while(rset.next()){
%>
    <form name="Editsurveyform" method="post" action="EditSurvey">
        <table>
            <tr><td>Family  name:</td><td><input type="text" name="familyName" value='<%=rset.getString(2)%>'></td></tr>
            <tr><td>First  name:</td><td><input type="text" name="firstName" value='<%=rset.getString(3)%>'></td></tr>
            <tr><td>Middle  name:</td><td><input type="text" name="middleName" value='<%=rset.getString(4)%>'></td></tr>
            <tr><td>Gender:</td>
                <td>
                    <%
                        if(rset.getString(5).equals("male")){
                    %>
                    <input type="radio" name="sex" value="male" checked>Male
                    <input type="radio" name="sex" value="female">Female
                    <%}
                        else{
                    %>
                     <input type="radio" name="sex" value="male">Male
                     <input type="radio" name="sex" value="female" checked>Female
                    <%
                        }
                    %>
                </td>
            </tr>
            <tr><td>Birthday:</td><td><input type="date" name="dob" value='<%=rset.getString(6)%>'></td></tr>
            <tr><td>Income : A?B?C?D?</td><td><input type="text" name="income" value='<%=rset.getString(7)%>'></td></tr>
            <tr><td>Complete Address:</td><td><input type="text" name="address" value='<%=rset.getString(8)%>'></td></tr>
            <tr><td>Coordinates:</td><td><input type="text" name="coordinates" value='<%=rset.getString(9)%>'></td></tr>
            <tr><td>Mobile number:</td><td><input type="tel" name="mobileno" value='<%=rset.getString(10)%>'></td></tr>
            <tr><td>Email Address:</td><td><input type="email" name="email" value='<%=rset.getString(11)%>'></td></tr>
            <tr><td>Present internet provider:</td><td><input type="text" name="iprovider" value='<%=rset.getString(12)%>'></td></tr>
            <tr><td>Positive comments with present provider:</td><td><textarea name="comments" cols="40" rows="5"><%=rset.getString(13)%></textarea></td></tr>
            <tr><td>Negative remarks with present provider:</td><td><textarea name="remarks" cols="40" rows="5"><%=rset.getString(14)%></textarea></td></tr>
            <tr><td><input type="submit" name="editsurvey" value="update survey details"></td><td><input type="reset" name="cancel" value="cancel"></td></tr>
        </table>

    </form>
<%}
    //response.sendRedirect("EditSurvey?surveyId="+surveyId);
%>
</body>
</html>

但我在下面的java中遇到了问题

package com.survey;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
 * Created by rajee on 2/18/15.
 */
public class EditSurvey extends HttpServlet {
    public String familyName;
    public String firstName;
    public String middlename;
    public String gender;
    public String dateOfBirth;
    public String income;
    public String completeAddress;
    public String coordinates;
    public String mobileno;
    public String emailAddress;
    public String presentIP;
    public String comment;
    public String remark;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String surveyId = (String) request.getAttribute("surveyid");
        System.out.println(surveyId);

        familyName = request.getParameter("familyName");
        firstName = request.getParameter("firstName");
        middlename = request.getParameter("middleName");
        gender = request.getParameter("sex");
        dateOfBirth = request.getParameter("dob");
        income = request.getParameter("income");
        completeAddress = request.getParameter("address");
        coordinates = request.getParameter("coordinates");
        mobileno = request.getParameter("mobileno");
        emailAddress = request.getParameter("email");
        presentIP = request.getParameter("iprovider");
        comment = request.getParameter("comments");
        remark = request.getParameter("remarks");
        System.out.println(familyName);
        System.out.println(firstName);
        System.out.println(middlename);
        System.out.println(gender);
        System.out.println(dateOfBirth);
        System.out.println(income);
        System.out.println(completeAddress);
        System.out.println(coordinates);
        System.out.println(mobileno);
        System.out.println(emailAddress);
        System.out.println(presentIP);
        System.out.println(comment);
        System.out.println(remark);

        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();

        Connection conn=null;
        String url="jdbc:mysql://localhost:3306/";
        String dbName="surveysample";
        String driver="com.mysql.jdbc.Driver";
        //String dbUserName="root";
        //String dbPassword="root";

//        try{
//
//
//            Class.forName(driver).newInstance();
//            conn = DriverManager.getConnection(url + dbName, "root", "root");
//            PreparedStatement pst =(PreparedStatement) conn.prepareStatement("insert into surveysample.surveydetail(familyname,firstname,middlename,gender,dateofbirth,income,complete_address,coordinates,mobilenumber,emailaddress,presentiprovider,comments,remarks) values (?,?,?,?,?,?,?,?,?,?,?,?,?)");//try2 is the name of the table
//
//            pst.setString(1,familyName);
//            pst.setString(2,firstName);
//            pst.setString(3,middlename);
//            pst.setString(4,gender);
//            pst.setString(5,dateOfBirth);
//            pst.setString(6,income);
//            pst.setString(7,completeAddress);
//            pst.setString(8,coordinates);
//            pst.setString(9,mobileno);
//            pst.setString(10,emailAddress);
//            pst.setString(11,presentIP);
//            pst.setString(12,comment);
//            pst.setString(13,remark);
//
//
//            int i = pst.executeUpdate();
//            //conn.commit();
//            String msg=" ";
//            if(i!=0){
//                msg="Record has been inserted";
//                pw.println("<font size='6' color=blue>" + msg + "</font>");
//
//
//            }
//            else{
//                msg="failed to insert the data";
//                pw.println("<font size='6' color=blue>" + msg + "</font>");
//            }
//            pst.close();
//        }
//        catch (Exception e){
//            pw.println(e);
//        }
//
   }

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

    }
}

代码

2 个答案:

答案 0 :(得分:0)

如果你的问题是surveyId为空是servlet,这是正常的,因为你试图从请求中获取它:

String surveyId = (String) request.getAttribute("surveyid");

当你把它放在会话中时,你应该从那里阅读:

String surveyId = (String) request.getSession().getAttribute("surveyid");

答案 1 :(得分:0)

您可以在servlet中尝试以下代码:

HttpSession session = request.getSession(false); 
String surveyId = (String) session.getAttribute("surveyid");

如果会话对象已存在,则此方法request.getSession(false)将不会创建新会话。它只会返回旧的会话对象,否则它将返回null。