开发者

Passing customized messages from Servlet to a JSP page?

I am new to JSP and Servlets.

What i want to know is the best w开发者_高级运维ay to pass some customized message to client web pages.

For example suppose i have a web page say student.jsp which has a form,to register a new student to our online application.after successfully inserting all the fields of the form, user submits the form and data is submitted to our servlet for further processing.Now,Servlet validates it and add it to our database.so,now servlet should send a message indicating a successful insertion of data entered by end user to end user (In our case student.jsp).

So,i could i pass this type of message to any client web page.

I don't want to pass this message as URL query String.

is there ant other better and secure way to pass these type of messages ...


use request.setAttribute("message", yourMessage) and then forward (request.getRequestDispatcher("targetPage.jsp").forward()) to the result page.

Then you can read the message in the target page via JSTL (<c:out value="${message}" />) or via request.getAttribute(..) (this one is not preferable - scriptlets should be avoided in jsp)

If you really need response.sendRedirect(..), then you can place the message in the session, and remove it after it is retrieved. For that you might have a custom tag, so that your jsp code doesn't look too 'ugly'.


I think it looks like this in JSTL:

<c:remove var="message" scope="session" />

I also think that, if "message" is a Java String, it can be set to the empty string after it's been used like this:

<c:set var="message" scope="session" value="" />

Actually, it also looks like it works if "message" is an array of Java Strings: String[]...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜