Home Java Servlet: request.SendRedirect doesn't work
Reply: 0

Java Servlet: request.SendRedirect doesn't work

donfontaine12
1#
donfontaine12 Published in 2018-02-13 12:03:21Z

I have a for loop that checks if any of the ID text inputs are the same. If it finds any duplicate ID numbers, it should redirect to the errorStudentInfo page.

I'm getting a NullPointException for line 40 on errorStudentInfo. Line 40 , which is the for loop to check for duplicate ID numbers and a redirect to the the same page.

import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;

@WebServlet("/getStudentGrade")
public class getStudentGrade extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request,
            HttpServletResponse response)
        throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        //out.println("Hello");

        String paramName1 = "";
        String paramName2 = "";
        String paramName3 = "";
        String[] paramValue1 = new String [5];
        String[] paramValue2 = new String [5];
        String[] paramValue3 = new String [5];

        String docType =
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
    "Transitional//EN\">\n";
        out.println(docType +
                "<HTML>\n" +
                "<HEAD><TITLE>Student Grades</TITLE></HEAD>\n" +
                "<BODY BGCOLOR=\"#FDF5E6\">\n" +
                "<H1 ALIGN=\"CENTER\"> Student Grades </H1>\n" +
                "<table border = '1'>" +
                "<tr>"
                + "<th>ID</th>"
                + "<th>Last Name</th>"
                + "<th>Grade</th></tr>");

        paramValue1 = request.getParameterValues("ID");
        paramValue2 = request.getParameterValues("lastName");
        paramValue3 = request.getParameterValues("grade");

        for (int i = 0; i < paramValue1.length; i++) {
            for (int j = i + 1; j < paramValue1.length; j++) {
                if (paramValue1[i].equals(paramValue1[j])) {
                    response.sendRedirect("/Project2/errorStudentInfo");
                }
            }
        }


        for (int i = 0; i < paramValue1.length; i++) {  
        if ((paramValue1 == null) || (paramValue1[i].trim().equals(""))
                || (paramValue3 == null) || (paramValue3[i].trim().equals(""))) 
         {
            response.sendRedirect("/Project2/errorStudentInfo");
         }

         ArrayList<String> paramNames = new ArrayList<String>();
         Enumeration enumeration = request.getParameterNames();

        while(enumeration.hasMoreElements()) 
        {
            paramName1 = (String)enumeration.nextElement();
            paramValue1 = request.getParameterValues(paramName1);
            if (paramName1.equals("ID") )
            {
                if ((paramName1 == null) || (paramValue1[i].trim().equals("")))
                {
                    response.sendRedirect("/Project2/errorStudentInfo");
                }else 
                    out.println("<tr><td>"+ paramValue1[i] +"</td>");
            }   
            paramName2 = (String)enumeration.nextElement();
            paramValue2 = request.getParameterValues(paramName2);

            if (paramName2.equals("lastName") ) {
            out.println("<td>"+ paramValue2[i] +"</td>");   
            }
            paramName3 = (String)enumeration.nextElement();
            paramValue3 = request.getParameterValues(paramName3);
            if (paramName1.equals("grade") )
            {
                if ((paramName3 == null) || (paramValue3[i].trim().equals("")))
                        response.sendRedirect("/Project2/errorStudentInfo");
            }
            out.println("<td>"+ paramValue3[i] +"</td></tr>");  
        }
        }
                out.println("</table>\n" +
                "</BODY></HTML>");
        out.println("</BODY></HTML>");
    }

    public void doPost(HttpServletRequest request,
            HttpServletResponse response)
                    throws ServletException, IOException {
        doGet(request, response);
    }
}


import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;

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

@WebServlet("/errorStudentInfo")
public class errorStudentInfo extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request,
            HttpServletResponse response)
        throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        //out.println("Hello");


        String docType =
        "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
        "Transitional//EN\">\n";
        String paramName1 = "";
        String paramName2 = "";
        String paramName3 = "";
        String[] paramValue1 = new String [5];
        String[] paramValue2 = new String [5];
        String[] paramValue3 = new String [5];
        String errorMsg = "";
        String errorMsg2 = "";
        String errorMsg3 = "";

        paramValue1 = request.getParameterValues("ID");
        paramValue2 = request.getParameterValues("lastName");
        paramValue3 = request.getParameterValues("grade");

        for (int i = 0; i < paramValue1.length; i++) {
            for (int j = i + 1; j < paramValue1.length; j++) {
                if (paramValue1[i].equals(paramValue1[j])) {
                    errorMsg3 = "Please enter five unique ID numbers: ";
                    response.sendRedirect("/Project2/errorStudentInfo");
                }
            }
        }


        for (int i = 0; i < paramValue1.length; i++) {
        paramValue1 = request.getParameterValues("ID");
        if ((paramValue1 == null) || (paramValue1[i].trim().equals(""))) 
        {
             errorMsg = "Enter a four-digit ID: ";
        }
        if (errorMsg.trim().equals(""))
        {

        ArrayList<String> paramNames = new ArrayList<String>();
        Enumeration enumeration = request.getParameterNames();

        while(enumeration.hasMoreElements()) 
        {
            paramName1 = (String)enumeration.nextElement();
            paramValue1 = request.getParameterValues(paramName1);
            errorMsg = "";
            if (paramName1.equals("ID")) {
                if ((paramName1 == null) || (paramValue1[i].trim().equals("")))
                        errorMsg = "Enter a four-digit ID: ";
            }
            out.println("<br>"+paramName1 +": " + request.getParameter(paramName1) + errorMsg);
        }
        paramName3 = (String)enumeration.nextElement();
        paramValue3 = request.getParameterValues(paramName3);
        if (paramName1.equals("grade") )
        {
            if ((paramName3 == null) || (paramValue3[i].trim().equals("")))
                errorMsg2 = "Enter a letter grade: ";
         }
        }
        else
        {
            //build html page
            String headerinfo = "";
            headerinfo = 

                    "<h1>Student Grades In</h1>" +
                    "<h2><font color = 'red'>"+ errorMsg3 +"</font></h2>" +
                    "<form method =\"post\" action =\"getStudentGrade\">\r\n" + 
                    "<table border = \"1\">\r\n" + 
                    "\r\n" + 
                    "<!-- Table Header  -->\r\n" + 
                    "\r\n" + 
                    "<tr>\r\n" + 
                    "<th>ID<font color = \"red\">*</font></th>\r\n" + 
                    "<th>Last Name</th>\r\n" + 
                    "<th> Grades<font color = \"red\">*</font></th>\r\n" + 
                    "</tr>\r\n" + 
                    "\r\n" + 
                    "<!-- Row Number 1  -->\r\n" + 
                    "\r\n" + 
                    "<tr>\r\n" + 
                    "<td>\r\n" + 
                    "<input type = \"text\" name =\"ID\" value = 
                    \"\" maxlength = \"4\">\r\n" + 
                    "<font color='red'><br>"+ errorMsg + "</font>" +
                    "</td>\r\n" + 
                    "<td>\r\n" + 
                    "<input type = \"text\" name =\"lastName\" value = 
                    \"\" maxlength = \"20\">\r\n" + 
                    "</td>\r\n" + 
                    "<td>\r\n" + 
                    "<select>\r\n" +
                    "<option value = \"\"></option>\r\n" +
                    "<option value = \"A\">A</option>\r\n" + 
                    "<option value = \"B\">B</option>\r\n" + 
                    "<option value = \"C\">C</option>\r\n" + 
                    "<option value = \"D\">D</option>\r\n" + 
                    "<option value = \"F\">F</option>\r\n" + 
                    "</select>\r\n" + 
                    "<font color='red'><br>"+ errorMsg2 + "</font>" +
                    "</td>\r\n" + 
                    "</tr>\r\n" + 
                    "\r\n" + 
                    "<!-- Row Number 2  -->\r\n" + 
                    "\r\n" + 
                    "<tr>\r\n" + 
                    "<td>\r\n" + 
                    "<input type = \"text\" name =\"ID\" value = 
                    \"\" maxlength = \"4\">\r\n" + 
                    "<font color='red'><br>"+ errorMsg + "</font>" +
                    "</td>\r\n" + 
                    "<td>\r\n" + 
                    "<input type = \"text\" name =\"lastName\" value =  
                    \"\" maxlength = \"20\">\r\n" + 
                    "</td>\r\n" + 
                    "<td>\r\n" + 
                    "<select>\r\n" + 
                    "<option value = \"\"></option>\r\n" +
                    "<option value = \"A\">A</option>\r\n" + 
                    "<option value = \"B\">B</option>\r\n" + 
                    "<option value = \"C\">C</option>\r\n" + 
                    "<option value = \"D\">D</option>\r\n" + 
                    "<option value = \"F\">F</option>\r\n" + 
                    "</select>\r\n" + 
                    "<font color='red'><br>"+ errorMsg2 + "</font>" +
                    "</td>\r\n" + 
                    "</tr>\r\n" + 
                    "\r\n" + 
                    "<!-- Row Number 3  -->\r\n" + 
                    "\r\n" + 
                    "<tr>\r\n" + 
                    "<td>\r\n" + 
                    "<input type = \"text\" name =\"ID\" value = 
                    \"\" maxlength = \"4\">\r\n" + 
                    "<font color='red'><br>"+ errorMsg + "</font>" +
                    "</td>\r\n" + 
                    "<td>\r\n" + 
                    "<input type = \"text\" name =\"lastName\" value = 
                    \"\" maxlength = \"20\">\r\n" + 
                    "</td>\r\n" + 
                    "<td>\r\n" + 
                    "<select>\r\n" + 
                    "<option value = \"\"></option>\r\n" +
                    "<option value = \"A\">A</option>\r\n" + 
                    "<option value = \"B\">B</option>\r\n" + 
                    "<option value = \"C\">C</option>\r\n" + 
                    "<option value = \"D\">D</option>\r\n" + 
                    "<option value = \"F\">F</option>\r\n" + 
                    "</select>\r\n" +
                    "<font color='red'><br>"+ errorMsg2 + "</font>" +
                    "</td>\r\n" + 
                    "</tr>\r\n" + 
                    "\r\n" + 
                    "<!-- Row Number 4  -->\r\n" + 
                    "\r\n" + 
                    "<tr>\r\n" + 
                    "<td>\r\n" + 
                    "<input type = \"text\" name =\"ID\" value = 
                    \"\" maxlength = \"4\">\r\n" + 
                    "<font color='red'><br>"+ errorMsg + "</font>" +
                    "</td>\r\n" + 
                    "<td>\r\n" + 
                    "<input type = \"text\" name =\"lastName\" value = 
                    \"\" maxlength = \"20\">\r\n" + 
                    "</td>\r\n" + 
                    "<td>\r\n" + 
                    "<select>\r\n" + 
                    "<option value = \"\"></option>\r\n" +
                    "<option value = \"A\">A</option>\r\n" + 
                    "<option value = \"B\">B</option>\r\n" + 
                    "<option value = \"C\">C</option>\r\n" + 
                    "<option value = \"D\">D</option>\r\n" + 
                    "<option value = \"F\">F</option>\r\n" + 
                    "</select>\r\n" + 
                    "<font color='red'><br>"+ errorMsg2 + "</font>" +
                    "</td>\r\n" + 
                    "</tr>\r\n" + 
                    "\r\n" + 
                    "<!-- Row Number 5  -->\r\n" + 
                    "\r\n" + 
                    "<tr>\r\n" + 
                    "<td>\r\n" + 
                    "<input type = \"text\" name =\"ID\" value =  
                    \"\"maxlength = \"4\">\r\n" + 
                    "<font color='red'><br>"+ errorMsg + "</font>" +
                    "</td>\r\n" + 
                    "<td>\r\n" + 
                    "<input type = \"text\" name =\"lastName\" value = 
                    \"\" maxlength = \"20\">\r\n" + 
                    "</td>\r\n" + 
                    "<td>\r\n" + 
                    "<select>\r\n" + 
                    "<option value = \"\"></option>\r\n" +
                    "<option value = \"A\">A</option>\r\n" + 
                    "<option value = \"B\">B</option>\r\n" + 
                    "<option value = \"C\">C</option>\r\n" + 
                    "<option value = \"D\">D</option>\r\n" + 
                    "<option value = \"F\">F</option>\r\n" + 
                    "</select>\r\n" + 
                    "<font color='red'><br>"+ errorMsg2 + "</font>" +
                    "</td>\r\n" + 
                    "</tr>\r\n" + 
                    "\r\n" + 
                    "<!-- End of Table -->\r\n" + 
                    "</table>\r\n" + 
                    "<br>\r\n" + 
                    "<input type = \"submit\" value=\"Submit\">\r\n" + 
                    "</form>";
            out.println(headerinfo);
        }

        }   
            out.println("</BODY></HTML>");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {
        doGet(request, response);
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.541266 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO