alessandro
alessandro

Reputation: 1721

Recognizing what JSP called the Servlet

I have two JSP pages that can access a Servlet. Now, I want when JSP 1 access the Servlet, Servlet will do some one kind of job (for example increment an int) and when JSP 2 will access the Servlet, it will do some other kind of job (for example decrement an int).

Can anybody help me by specifying how is it possible ?

Upvotes: 1

Views: 307

Answers (3)

KV Prajapati
KV Prajapati

Reputation: 94645

You could pass different value on same parameter.

In page1.jsp

<jsp:forward page="/MyServelt">
   <jsp:param name="action" value="incr" /> 
</jsp:forward/>

In page2.jsp

<jsp:forward page="/MyServelt">
   <jsp:param name="action" value="decr" /> 
</jsp:forward/>

If <form> tag is used then add <input type="hidden"/>,

<form method="post" action="/MyServlet">
  <input type="hidden" name="action" value="incr"/>
  .....
</form>

Upvotes: 3

Acn
Acn

Reputation: 1066

use synchronous keyword to the method that does the increment or decrement operation.

This is more like isolation in database transaction. Secondly to decide what exactly to do,

You can use the request parameters eg - domain/myservlet?action=increment

:)

Upvotes: 1

Harry Joy
Harry Joy

Reputation: 59660

First of all Servlets are not page.

Second, solution to your problem will be to pass a parameter in request from jsp page to servlet that helps in identifying from where the request has been made and by using this parameter decide which operation you should do or you can also use referer header of request or getRequestURI() method of request to get it but I have never used it so not sure about it.

Upvotes: 1

Related Questions