JSP getParameters

时间:2013-04-27 22:15:51

标签: java jsp

我有一个简单的JSP项目,它有3个类,控制器和DAO 我可以登录但是当我点击我的按钮时,我无法从表单中获取参数。

Lemme显示代码,也许你会弄清楚:

package school.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import school.dao.StudentDAO;
import school.model.Student;

public class StudentController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static String UPDATE = "/student.jsp";
    private static String VIEW_COURSES = "CourseController";
    private static String VIEW_GRADES =  "GradeController";
    private StudentDAO dao;

    public StudentController() {
        dao = new StudentDAO();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String forward = "";
        String action = request.getParameter("name");

        if (action.equalsIgnoreCase("update")) {

            forward = UPDATE;
            Student student = (Student) request.getAttribute("student");
            request.setAttribute("student", student);

        } else if (action.equalsIgnoreCase("viewgrades")) {

            forward = VIEW_GRADES;

            Student student = (Student) request.getAttribute("student");
            request.setAttribute("student", student);

        } else if (action.equalsIgnoreCase("viewcourses")) {

            forward = VIEW_COURSES;
            Student student = (Student) request.getAttribute("student");
            request.setAttribute("student", student);
        }

        RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
    }

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

        doGet(request, response);
    }
}

这是WEB.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>School_JSP</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>CourseController</display-name>
    <servlet-name>CourseController</servlet-name>
    <servlet-class>school.controller.CourseController</servlet-class>
  </servlet>
  <servlet>
    <description></description>
    <display-name>StudentController</display-name>
    <servlet-name>StudentController</servlet-name>
    <servlet-class>school.controller.StudentController</servlet-class>
  </servlet>
  <servlet>
    <description></description>
    <display-name>LoginServlet</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>school.controller.LoginServlet</servlet-class>
  </servlet>

  <servlet-mapping>
        <servlet-name>StudentController</servlet-name>
        <url-pattern>/StudentController</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>CourseController</servlet-name>
        <url-pattern>/CourseController</url-pattern>
    </servlet-mapping>
</web-app>

和mainPage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Main Page</title>
</head>
<body>

    <h3>
        Welcome,
        <c:out value="${student.firstName}" />

        </br>
        <h1>My Personal Information</h1>
        <center>
            <form method="POST" action='StudentController' name="viewgrades">
                <input type="submit" value="View My Grades" />
            </form>
            <form method="POST" action='StudentController' name="viewcourses">
                <input type="submit" id="hidden" value="View My Courses" />
            </form>
            <form method="POST" action='StudentController' name="update">
                <input type="submit" id="hidden" value="Update Personal Information" />
            </form>
        </center>
    </h3>

</body>
</html>

2 个答案:

答案 0 :(得分:2)

你的属性和参数令人困惑。

参数是浏览器发送的字符串值。可以使用getParameterXxx()系列方法访问它们。

属性是可以添加到请求(或会话或servlet上下文)或任何类型的数据,以便使它们成为后代。您通常在servlet中向请求添加属性(例如,有关登录用户的信息),以便在JSP中返回此属性并显示信息。

在您的代码中,唯一获得参数的地方已被注释掉。并且您正在尝试获取名为“hidden”的参数,尽管表单中没有一个输入字段具有名称。它是浏览器提交的输入字段的name属性,而不是其id属性。

此外,对于获取或阅读内容的操作,您应该使用GET而不是POST。

答案 1 :(得分:0)

您没有获得任何值的原因是表单中的属性“name”,实际上只是表单的名称,根本不发送。 要实际发送数据,您需要在表单的innerHTML中提供一些。像这样(不是最好的解决方案,但是你应该跑步):

<form method="GET" action="StudentController" name="formName">
  <input type="hidden" name="viewgrades" value="show" />
  <input type="submit" value="View My Grades" />
</form>

在此之后,您可以在Java类中读取发送的数据“viewgrades = show”(参数=值)。

另外,顺便说一句,你不应该使用“h3”作为段落(“p”)或部分。它的目的是成为标题。