附加到StringBuffer(Java Servlet)不在Servlet中作为属性拾取

时间:2014-10-04 08:38:41

标签: java jsp servlets

由于某些原因,我在doGet方法中附加到StringBuffer(sb)的代码HTML没有在另一个servlet中被拾取。

这是我附加到StringBuffer sb的HTML代码:

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    ServletContext context = request.getSession().getServletContext();
    List<Course> courses = (List<Course>) context.getAttribute("COURSES");

    StringBuffer sb = new StringBuffer();
    sb.append(getHeader());
    sb.append("<form action=\"/Assignment_2/addCourse\">");
    sb.append("<table border=\"2\">");
    //sb.append("<tr><td>");

    //sb.append("</tr></td>");
    sb.append("<select>"
            + "<option value = book>Book</option>"
            + "<option value=tablet>Tablet</option>"
            /*+ "<option value=xxxxxx>XXXXXXX</option>"
            + "<option value=yyyyy>YYYYY</option>"*/
            + "/select>");
    sb.append("<tr><td>Type</td><td><input type=\"text\" name=\"type\" value=\"\" /></td></tr>");
    sb.append("<tr><td>Name</td><td><input type=\"text\" name=\"name\" value=\"\" /></td></tr>");
    sb.append("<tr><td>Additional Info</td><td><input type=\"text\" info=\"Additional Info\" value=\"\" /></td></tr>");
    sb.append("<tr><td># of Copies</td><td><input type=\"text\" name=\"# of Copies\" value=\"\" /></td></tr>");

    /*for (Course course : courses) {
        sb.append("<input type=\"checkbox\" name=\"prerequisite\" value=\"" + course.getCode() + "\">" + course.getCode() + "</input> <br />");
    }*/


    sb.append("</td></tr><tr><td><input type=\"submit\" value=\"Add\" /></td></tr></table></form>");
    sb.append("</body></html>");
    PrintWriter out = response.getWriter();
    out.println(sb.toString());
}

public String getHeader() {
    StringBuffer sb = new StringBuffer();
    sb.append("<html>");
    sb.append("<head><title>Department Library</title></head><body>");
    return sb.toString();
}

因此,在用户单击“提交”后,代码将显示在此处:

    /**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ServletContext context = request.getSession().getServletContext();
    List<Course> courses = (List<Course>) context.getAttribute("COURSES");
    for (Course course : courses) {
        System.out.println(course);
    }

    StringBuffer sb = new StringBuffer();
    sb.append(getHeader());

    sb.append("<table border=\"2\"><tr>"
            + "<td>ID</td>"
            + "<td>Type</td>"
            + "<td>Name</td>"
            + "<td>Additional Info</td>"
            + "<td>Available</td>"
            + "<td>Operation</td></tr>");
    for (Course course : courses) {
        sb.append("<tr><td>");
        sb.append(course.getMyId() + "</td><td>" + course.getType() + "</td>");
        /*sb.append("<td>");
        for (String str : course.getPrerequisites()) {
            sb.append(str + "  ");
        }*/
        sb.append("</td><td>"
                + course.getName());

        sb.append("</td><td>"
                + course.getAdditionalInfo());
        sb.append("</td><td>"
                + course.getAvailable());

        sb.append("</td><td><a href=\"edit?code="+course.getCode() +"\">Edit</a></td>");//</tr>");



    }
    sb.append("</table><a href=\"add\">Add Items</a></body></html>");


    PrintWriter out = response.getWriter();
    out.println(sb.toString());
}

public String getHeader() {
    StringBuffer sb = new StringBuffer();
    sb.append("<html>");
    sb.append("<head><title>Department Library</title></head><body>");
    return sb.toString();
}

这一行:

for (Course course : courses) {
        System.out.println(course);
    }

告诉我我的数据很好...... Servlets发生了什么?

************* EDIT ****************

添加以下内容(在我原始帖子的第一种方法中):

sb.append("<tr><td>Available</td><td><input type=\"text\" name=\"available\" value=\"\" /> </td></tr>");

到doGet我在表格中收到了这些数据!但出于某种原因(在同一个文件中):

sb.append("<tr><td>Additional Info</td><td><input type=\"text\" name=\"info\" value=\"\" /> </td></tr>");

未被读取,例如,它在我的表中读为“null”。我没有为输入键入'null'(只是为了澄清)。

此外..点击下一步/提交/添加提交表单操作输入的内容后,我的URL中可以看到我的数据/输入。

2 个答案:

答案 0 :(得分:1)

in  first method...

 /*for (Course course : courses) {
        sb.append("<input type=\"checkbox\" name=\"prerequisite\" value=\"" + course.getCode() + "\">" + course.getCode() + "</input> <br />");
    }*/

in second method

String [] temp=request.getparameterbyValues("prerequisite");

 for (i=0;i<=temp.length;i++) {

        System.out.println(temp[i]);
    }

答案 1 :(得分:0)

您没有在第二个servlet中使用请求对象的值。您只是从上下文对象中读取值。