0

Below is code for a simple Servlet(Process.java), JSP page(index.jsp) and Model(Model.java).

index.jsp:

<%@ page import="com.example.*" %>

<html>
<head>
<title> Myapp </title>
</head>

<body>
<form action="process.do" method="POST">
UserName: <input type="text" name="username">
<br>
UserID: <input type="text" name="userid">
<br>
<input type="submit">
<br>

<%
Model m = (Model) request.getAttribute("model");

if( m != null) {
out.println("Username: " + m.getUserName() );
out.println("UserID: " + m.getUserID() );
}
%>

</form>
</body>
</html>

Process.java:

package com.example;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Process extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Model m = new Model();
        m.setUserName( request.getParameter("username") );
        m.setUserID( Integer.parseInt( request.getParameter("userid") ) );

        request.setAttribute("model", m);
        response.sendRedirect( request.getRequestURI() );
    }
}

Model.java:

package com.example;

public class Model {

    private String userName = "";
    private int userID = -1;

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setUserID(int userID) {
        this.userID = userID;
    }

    public String getUserName() {
        return userName;
    }

    public int getUserID() {
        return userID;
    }
}

web.xml:

<servlet>
    <servlet-name>Process</servlet-name>
    <servlet-class>com.example.Process</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Process</servlet-name>
    <url-pattern>/process.do</url-pattern>
</servlet-mapping>

I am using Tomcat7 and I have deployed this app in context /myapp. I am able to view index.jsp page correctly, but when I submit the form, I am getting below error:

HTTP Status 405 - HTTP method GET is not supported by this URL
anilmwr
  • 477
  • 9
  • 16
  • Edit: My goal here is to redirect to original index.jsp page from the Process servlet. How to achieve this? – anilmwr Jan 29 '13 at 12:56

3 Answers3

0

What you are trying to accomplish with response.sendRedirect( request.getRequestURI() ); ?

This will instruct browser to send a GET request to /process.do which is not handled in servlet(You are extending only doPost). If you want to go back to index.jsp just have response.sendRedirect( "index.jsp");

EDIT:

Because you want model attribute to be accessed in index.jsp, we cant actually do a browser redirect. Instead a server redirect is needed.

request.getRequestDispatcher("index.jsp").include(request, response) instead of response.sendRedirect() should work for you.

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42
  • My goal here is to redirect to original index.jsp page. How can I achieve that? – anilmwr Jan 29 '13 at 12:53
  • `response.sendRedirect("index.jsp")` incase you dont want the hardcoding, pass this return url as a parameter during form submit itself – Subin Sebastian Jan 29 '13 at 12:55
  • Thanks! It did redirect to index.jsp, but my model is not accessible to index.jsp, I am seeing same empty form after submit. – anilmwr Jan 29 '13 at 13:00
  • In that case take a look into `RequestDispatcher` – Subin Sebastian Jan 29 '13 at 13:03
  • I am updating answer with that. – Subin Sebastian Jan 29 '13 at 13:04
  • I tried using RequestDispatcher, but then my browser is showing URL of process.do. I need it to go to index.jsp. – anilmwr Jan 29 '13 at 13:11
  • 1
    You cant have both, if you need your url to point to index.jsp, that means it is a result of direct get request from browser. Another option is to use session. Ie use response.sendRedirect('index.jsp') but also use `request.getSession().setAttribute()` instead of `request.setAttribute` and `request.getSession().getAttribute()`, this will ensure model is preserved across multiple requests. – Subin Sebastian Jan 29 '13 at 13:20
0

You are not redirecting to JSP. Let me explain what happens is when you post the form url changes to http://domain.com/../process.do and when you use request.getRequestedUri() it gives to the url of servlet and it doesn't have doGet() method hence you are getting that error. You should use response.sendRedirect("index.jsp") to redirect to your index.jsp file .

kaysush
  • 4,797
  • 3
  • 27
  • 47
0

When ,you use the "response.sendRedirect()" method ,it will handover the request object to the browser .so,you can't handle request object any more.Use "RequestDispacther" instead of "sendRedirect".I hope ,this may give you a clarity.Don't Mind,if iam not reach you.