0

i hava a html page and hello is my servlet

 <html>
 <body>
 <form action="http://localhost:8080/nitin/jsnipop/hello" method="post">
   <input type="text" id="foo" name="foo">
 <input type="submit" value="Send" name="submit" id="submit">
</form>
 </body>
 </html>

and my servlet is

public class hello  extends HttpServlet{
/**
 * 
 */
private static final long serialVersionUID = 1L;

public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
    res.setContentType("text/html");
    PrintWriter pw=res.getWriter();
    String foo=req.getParameter("foo");   
    pw.println("Welcome "+foo);
}
}

now i want to send foo string variable on client side and set this as a user name my client class name is ser.java please help

1 Answers1

0

Note:

  • Don't write anything in response stream once request is forwarded to some one else.

There is no meaning of below line:

pw.println("Welcome "+foo);

because now the response is driven by forwarded JSP/Servlet/HTML.

  • Classes that is shared between client and server must be placed under shared folder.

Steps to follow:

  • Create an object of Model class that is placed under shared folder and just a POJO class.
  • Set the properties in the object such as username etc.
  • Set the object as request attribute.
  • Forward the request to some one else
  • Simply read it in forwarded JSP/Servlet.

Please have a look at below links to set and get the request attribute


Sample code:

hello Servlet (server side)

// get the parameter from the request
String foo=req.getParameter("foo");

// create an object of Model class
Model model = new Model();

// Set the properties in the object
model.setUserName(foo);

// set the object as request attribute
req.setAttribute("model",model);

// forward the request to some one else
rd.forward(req, res); 

In forwarded JSP (using Scriplets)

<!-- Simply read it here -->
<%com.x.y.shared.Model model = request.getAttribute("model");%>

Model class (shared folder)

public class Model implements IsSerializable, Serializable{
    private String userName;

    public Model(){} // no-arg constructor
    // getter & setter
}
Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • i want to set this in a text box and my have onmodule load function is this work –  May 09 '14 at 12:09
  • I have already shared you code. What was the issue with that code? – Braj May 09 '14 at 12:09
  • i can't use jsp help me with some other option. It will be vry kind of you –  May 09 '14 at 12:13
  • OK please tell me what is `rd` is here? where your request is forwarded? – Braj May 09 '14 at 12:15
  • its a object of dispeture class –  May 09 '14 at 12:17
  • I know it but where is it dispatching the request? – Braj May 09 '14 at 12:18
  • Tell me the flow. **HTML -> Servlet -> ?(What next here)** – Braj May 09 '14 at 12:19
  • my flow is HTMl -> Servlet-> model.java ( its a client Javaclass) –  May 12 '14 at 05:26
  • after following the above procedure i face (java.lang.NoClassDefFoundError: com/google/gwt/core/client/EntryPoint) this exception. –  May 12 '14 at 05:57
  • Please search on Google. It's very common issue in GWT. – Braj May 12 '14 at 07:09
  • Sorry you can't do it. Model is just like a DTO (Data Transfer Object) that is used to transfer the data between server and client. HTML -> Servlet(set model as request attribute) -> HTML/JSP(read model from the request) – Braj May 12 '14 at 07:13