0

I have a JSP file that refers to CSS. My JSP has a form that redirects to the servlet .

The servlet does some heavy database queries etc.

Then the data is shown on the servlet page

However since the servlet does not have any formatting , the data is displayed without any formatting . How can I get the formatting from that jsp file and apply it to servlet .

I could use

out.println(".. )

but this is not convinient.

4
  • 2
    ... Forward to a JSP page that displays the results? Commented Feb 27, 2014 at 19:42
  • Thank you for reply . So my servlet test.java should have some code that forwards data to the original jsp ? So my servlet becomes a simple class right ? Commented Feb 27, 2014 at 19:44
  • stackoverflow.com/tags/servlets/info Commented Feb 27, 2014 at 19:59
  • @Buras More or less, but what Chris says is correct; if this is a form submission then you should use the post-redirect-get pattern. Commented Feb 27, 2014 at 20:35

3 Answers 3

1

Here's one way to do this: 1) put the content you want to show in a request scope. You can do this using setAttribute() of request. Eg, request.setAttribute("mycontent", <your actual content>)

2) "your actual content" can be any data structure. Choose one that fits your needs.

3) In your second JSP, use this data structure. YOu can do, request.getAttribute("mycontent") (dont forget the cast. This returns Object)

4) You can apply whatever CSS you have to this content.

Cheers!

Sign up to request clarification or add additional context in comments.

Comments

1

Create another JSP page to which this servlet dispatches response, add your CSS there, use the JSP to render pages the way you want.

I suggest you go through MVC pattern for creating your web application.

see this question

Comments

0

You shouldn't just have the servlet forward to the JSP. Since the last action known to the browser is the post from the form, a refresh of the page to which you were forwarded causes another post to happen. You probably don't want that.

The correct approach is to post to a servlet which performs the update logic. The servlet then does a send-redirect (status 302?) back to the browser with the url of the follow-on page. The browser then does a get for that page and a subsequent refresh will work correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.