Post Requests in java servlet

J

jonathan Cook

All,

The first time I make a request to my servlet this calls the doGet
method which builds a page using XML and XSLT depending on the
identity and Authorisation the user has.

A form comes back to client and when submitting this form the doPost
method is called which provides several responses the correct one
being a page which allows the user to make some further selections and
submit which will then call the doPost method again. Consequently
there is a lot of logic in the doPost method.

I understand that I could have more than one servlet but ideally I
would like to have some sort of context associated with each post
request so I can tell whether it is the post from a form or from radio
buttons etc and then chose which code to execute correspondingly.

Would appreciate some help because if the doPost method calls
printCarInformation then it will everytime there is post. I could get
round this by checking input parameters but its all a bit messy.

Thanks
Jonathan Cook

For example this is what the doPost method calls:

private void printCarInformation(HttpServletRequest req,
HttpServletResponse res) throws ServletException, ServiceException,
ParserConfigurationException, SAXException, IOException,
TransformerException
{
// retrieve a PNR
//set HttpServletResponse type
PrintWriter out = res.getWriter();
String pnrLocator = null;
pnrLocator = req.getParameter("pnrlocator");

//System.out.println(pnrLocator.length());

if (pnrLocator.length() == 0 || pnrLocator == null)
{
res.setContentType("text/xml");
String htmlOutput1 = xmlDocManip.getDocAsString();
String XSLTStyleSheet =
"http://localhost:8080/BuildErrorPagePNR.xsl";

String xslDeclaration = "<?xml-stylesheet type=\"text/xsl\"
href=\"" + XSLTStyleSheet + "\"?>";
String xmlVersion = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
String Output = xmlVersion + "\n" + xslDeclaration +"\n" +
htmlOutput1;

out.write(Output);
out.close();
}
else
{
res.setContentType("text/xml");
CarVoucherBusinessLogic bs = new CarVoucherBusinessLogic();
Element PNR = bs.retrievePNR(pnrLocator);

Document doc = bs.buildCarSegments(PNR);

String XSLTStyleSheet = "http://localhost:8080/style2.xsl";

Element rootElement = doc.getDocumentElement();
String element = XMLUtils.ElementToString(rootElement);

String xslDeclaration = "<?xml-stylesheet type=\"text/xsl\"
href=\"" + XSLTStyleSheet + "\"?>";
String xmlVersion = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
String htmlOutput = xmlVersion + "\n" + xslDeclaration +"\n" +
element;

//output html to page
//htmlOutput = xmlDocManip.applyTransformationUsingBrowser();
out.write(htmlOutput);
}
}
 
O

Oscar kind

jonathan Cook said:
The first time I make a request to my servlet this calls the doGet
method which builds a page using XML and XSLT depending on the
identity and Authorisation the user has.

A form comes back to client and when submitting this form the doPost
method is called which provides several responses the correct one
being a page which allows the user to make some further selections and
submit which will then call the doPost method again. Consequently
there is a lot of logic in the doPost method.

Personally, I use the same method for the GET and POST requests, and
decide on the content (and presence) of the various form variables what to
do. Usually calling another class.

This enables me to test requests with specific sets of parameters without
creating a test page (I'm lazy). It also forces me to cleanup my design,
as otherwise the code will become unreadable.

I understand that I could have more than one servlet but ideally I
would like to have some sort of context associated with each post
request so I can tell whether it is the post from a form or from radio
buttons etc and then chose which code to execute correspondingly.

This is a good idea, and is a basis for the command pattern. It is also
used by Struts (a framework that does a the above in a generic way).

Would appreciate some help because if the doPost method calls
printCarInformation then it will everytime there is post. I could get
round this by checking input parameters but its all a bit messy.

You could create one servlet for each group of information that is passed
in the form. Then use the command pattern, i.e. create a specific class
to handle the request (the different classes would implement the same
interface / extend the same superclass).

If this becomes unusable because of the sheer number of decision paths, I
find it is time to switch to a complete framework (Struts being my
favourite, but there are others).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top