Gabriel Câmara
Gabriel Câmara

Reputation: 1249

JSP getParameters

I have a simple JSP project, which has 3 classes, controllers and DAO's I can Login but when I click on my buttons, I just can't get the Parameters from the form.

Lemme show the code, and maybe You'll figure it out:

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);
    }
}

And here's the 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>

And the 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>

Upvotes: 0

Views: 418

Answers (2)

Andreas Gnyp
Andreas Gnyp

Reputation: 1840

The reason you don't get any values is that the attribute "name" in the form, indeed is just the name of the form and isn't sent at all. To actually send data, you need to provide some in the innerHTML of the form. Like this (not the best solution, but should you get running):

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

After this you can read the sent data "viewgrades=show" (parameter=value) in your Java-class.

Also, btw, you shouldn't use "h3" as a paragraph ("p") or section. It is intended to be a headline.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691635

You're confusing attributes and parameters.

Parameters are String values sent by the browser. They're accessed using the getParameterXxx() family of methods.

Attributes are data that you can add to a request (or session, or servlet context), or any type, in order to get them backlater. You typically add an attribute to the request in a servlet (for example, the information about the logged in user) in order to get back this attribute in the JSP and display the information.

In your code, the only place where you get a parameter is commented out. And you're trying to get a parameter named "hidden", although none of the form has an input field with than name. It's the name attribute of an input field that is submitted by the browser, and not its id attribute.

Also, you should use GET rather than POST for actions which consist in getting or reading things.

Upvotes: 2

Related Questions