Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

send request body while invoke servlet

Posted on 2011-10-28
6
Medium Priority
?
299 Views
Last Modified: 2012-05-12
I want invoke a servlet which will read the request body of the request call. Basically, I need to make a request to this servlet and send a string in the request body, the servlet on receiving will read the string. How do I acheive this?
0
Comment
Question by:greenearth
  • 2
  • 2
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 37048091
0
 

Author Comment

by:greenearth
ID: 37051267
I will try to work with this sample and see how far i got to this..
0
 
LVL 28

Accepted Solution

by:
rrz earned 2000 total points
ID: 37078643
Here is an example. First, a test JSP.
<html>
<body>
<form action="read" method="post">
<input type="text" name="message" value="test"/>
<input type="submit" />
</form>
</body>
</html>

Open in new window

 
package rrz;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ReadBodyServlet extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        BufferedReader reader = request.getReader();
        String line;
        while((line = reader.readLine()) != null){
            response.getWriter().write(line);
        }
    }
}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 28

Expert Comment

by:rrz
ID: 37078669
    <servlet>
       <servlet-name>read</servlet-name> 
       <servlet-class>rrz.ReadBodyServlet</servlet-class> 
  </servlet>
  <servlet-mapping>
    <servlet-name>read</servlet-name>
    <url-pattern>/read</url-pattern>
  </servlet-mapping>

Open in new window

0
 

Author Closing Comment

by:greenearth
ID: 37079855
Yes this make sense. I was making myself over complicated
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37079872
>>the servlet on receiving will read the string

The accepted answer is not doing that though ...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question