School Schedule Application..Please Help [message #127165] |
Thu, 28 July 2005 23:53  |
Eclipse User |
|
|
|
Originally posted by: arupsarkar.yahoo.com
I am getting the following error while doing the schollschedule
application during the execution of the servlet
23:48:04,907 ERROR [[Schedule]] Servlet.service() for servlet Schedule
threw exception
java.lang.IllegalStateException: Cannot forward after response has been
committed
The following is my code, can anybody please help me, I am pretty much
stuck here...
package org.eclipse.wtp.sample.classschedule;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author asarkar
*
*/
public class ScheduleServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
/* (non-Javadoc)
* @see
javax.servlet.http.HttpServlet#doPost(javax.servlet.http.Htt pServletRequest,
javax.servlet.http.HttpServletResponse)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(request, response);
String title = request.getParameter("title");
int starttime = Integer.parseInt(request.getParameter("starttime"));
int endtime = Integer.parseInt(request.getParameter("endtime"));
String[] days = request.getParameterValues("day");
System.out.println("Inside the servlet doPost method. Before");
//SchoolSchedule schedule =
(SchoolSchedule)request.getSession(true).getAttribute("schoolschedule ");
SchoolSchedule schedule =
(SchoolSchedule)request.getAttribute("schoolschedule");
System.out.println("Inside the servlet doPost method. After");
if(schedule == null)
{
schedule = new SchoolSchedule();
//request.getSession().setAttribute("schoolschedule", schedule);
//getServletContext().getRequestDispatcher("/Schedule.jsp").forward(request,
response);
}
if(days != null)
{
for(int i = 0; i < days.length; i++)
{
String dayString = days[i];
int day;
if(dayString.equalsIgnoreCase("SUN")) day = 0;
else if(dayString.equalsIgnoreCase("MON")) day = 1;
else if(dayString.equalsIgnoreCase("TUE")) day = 2;
else if(dayString.equalsIgnoreCase("WED")) day = 3;
else if(dayString.equalsIgnoreCase("THU")) day = 4;
else if(dayString.equalsIgnoreCase("FRI")) day = 5;
else day = 6;
SchoolClass clazz = new SchoolClass(title, starttime, endtime, day);
schedule.addClass(clazz);
}
}
//request.getSession().setAttribute("schoolschedule", schedule);
request.setAttribute("schoolschedule", schedule);
//getServletContext().getRequestDispatcher("/Schedule.jsp").forward(request,
response);
request.getRequestDispatcher("/Schedule.jsp").forward(request, response);
}
}
|
|
|
Re: School Schedule Application..Please Help [message #127314 is a reply to message #127165] |
Fri, 29 July 2005 08:44   |
Eclipse User |
|
|
|
In the case of extending HttpServlet, you don't want to call
super.doPost() or super.doGet(). The default implementation for
doPost() and doGet() returns an error response indicating that method is
not supported. For example, if doGet() was called on your servlet
below, this would the appropriate response since you haven't overridden
doGet() with a "real" implementation. You don't see the default
doPost() response because the forward() overrides that response with a
new error.
Cheers,
Larry
Arup Sarkar wrote:
> I am getting the following error while doing the schollschedule
> application during the execution of the servlet
>
> 23:48:04,907 ERROR [[Schedule]] Servlet.service() for servlet Schedule
> threw exception
> java.lang.IllegalStateException: Cannot forward after response has been
> committed
>
> The following is my code, can anybody please help me, I am pretty much
> stuck here...
>
> package org.eclipse.wtp.sample.classschedule;
>
> import java.io.IOException;
> import javax.servlet.ServletException;
> import javax.servlet.http.HttpServlet;
> import javax.servlet.http.HttpServletRequest;
> import javax.servlet.http.HttpServletResponse;
>
> /**
> * @author asarkar
> *
> */
> public class ScheduleServlet extends HttpServlet{
>
> /**
> * */
> private static final long serialVersionUID = 1L;
>
> /* (non-Javadoc)
> * @see
> javax.servlet.http.HttpServlet#doPost(javax.servlet.http.Htt pServletRequest,
> javax.servlet.http.HttpServletResponse)
> */
> protected void doPost(HttpServletRequest request,
> HttpServletResponse response) throws ServletException, IOException {
> // TODO Auto-generated method stub
> super.doPost(request, response);
>
> String title = request.getParameter("title");
> int starttime =
> Integer.parseInt(request.getParameter("starttime"));
> int endtime = Integer.parseInt(request.getParameter("endtime"));
> String[] days = request.getParameterValues("day");
>
> System.out.println("Inside the servlet doPost method. Before");
> //SchoolSchedule schedule =
> (SchoolSchedule)request.getSession(true).getAttribute("schoolschedule ");
> SchoolSchedule schedule =
> (SchoolSchedule)request.getAttribute("schoolschedule");
> System.out.println("Inside the servlet doPost method. After");
> if(schedule == null)
> {
>
> schedule = new SchoolSchedule();
> //request.getSession().setAttribute("schoolschedule",
> schedule);
>
> //getServletContext().getRequestDispatcher("/Schedule.jsp").forward(request,
> response);
> }
>
> if(days != null)
> {
> for(int i = 0; i < days.length; i++)
> {
> String dayString = days[i];
> int day;
> if(dayString.equalsIgnoreCase("SUN")) day = 0;
> else if(dayString.equalsIgnoreCase("MON")) day = 1;
> else if(dayString.equalsIgnoreCase("TUE")) day = 2;
> else if(dayString.equalsIgnoreCase("WED")) day = 3;
> else if(dayString.equalsIgnoreCase("THU")) day = 4;
> else if(dayString.equalsIgnoreCase("FRI")) day = 5;
> else day = 6;
>
> SchoolClass clazz = new SchoolClass(title, starttime,
> endtime, day);
> schedule.addClass(clazz);
> }
> }
>
> //request.getSession().setAttribute("schoolschedule", schedule);
> request.setAttribute("schoolschedule", schedule);
>
> //getServletContext().getRequestDispatcher("/Schedule.jsp").forward(request,
> response);
> request.getRequestDispatcher("/Schedule.jsp").forward(request,
> response);
> }
>
> }
>
>
|
|
|
|
Re: School Schedule Application..Please Help [message #128361 is a reply to message #127821] |
Mon, 01 August 2005 10:13  |
Eclipse User |
|
|
|
The URL you are using will send a request using the GET method, not the
POST method. The response you see is the default response provided by
the HttpServlet for doGet() indicating that your servlet hasn't
implemented its own response for doGet().
Note that the ScheduleServlet in the tutorial is not intended to be
accessed directly with a URL like you show. So the response you see is
normal. Instead, Schedule.jsp sends requests to ScheduleServlet using
the POST method so you won't see this problem.
Adding a doGet() to the ScheduleServlet with some "error handling" to
provide a more user friendly response would be good programming
practice, but is beyond the scope of the tutorial.
Cheers,
Larry
Arup Sarkar wrote:
> Larry: Thank you very much for your reply. Now there is no error, please
> excuse my ignorance I am in the learning phase now, anyway I have
> attached the web.xml when I am trying to put the
> http://localhost:8080/SchoolSchedule/Schedule I get the following error.
>
> HTTP method GET is not supported by this URL
>
> Here is my web.xml
>
> <?xml version="1.0" encoding="UTF-8"?>
> <web-app id="WebApp_ID" version="2.4"
> xmlns="http://java.sun.com/xml/ns/j2ee"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
> http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
> <display-name>SchoolSchedule</display-name>
> <welcome-file-list>
> <welcome-file>index.html</welcome-file>
> <welcome-file>index.htm</welcome-file>
> <welcome-file>index.jsp</welcome-file>
> <welcome-file>default.html</welcome-file>
> <welcome-file>default.htm</welcome-file>
> <welcome-file>default.jsp</welcome-file>
> </welcome-file-list>
> <servlet>
> <description></description>
> <display-name>Schedule</display-name>
> <servlet-name>Schedule</servlet-name>
> <servlet-class>
> org.eclipse.wtp.sample.classschedule.ScheduleServlet
> </servlet-class>
> </servlet>
> <servlet-mapping>
> <servlet-name>Schedule</servlet-name>
> <url-pattern>/Schedule</url-pattern>
> </servlet-mapping>
> </web-app>
>
>
>
|
|
|
Powered by
FUDForum. Page generated in 0.04551 seconds