aspose file tools*
The moose likes Java in General and the fly likes core java communicates with servlet or jsp? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "core java communicates with servlet or jsp?" Watch "core java communicates with servlet or jsp?" New topic
Author

core java communicates with servlet or jsp?

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,

I have a core java code in the path /var/lib/asterisk/agi-bin. Then I have a servlet code in the path /usr/local/apache-tomcat/webapps/Inbound/....Is it possible to pass values from the core java code to the servlet code.

Thanks
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
Preethi,
Can you elaborate on what you are trying to do? You want to use the java code in the web context?


Cheers!
RSR
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
Are these codes running in different JVMs?


Ping & DNS - my free Android networking tools app
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks for your replies.Actually both the java code is done using asterisk java and what it will do is plays the ivr and changes a value 0 to 1 in a field say status in the database. The servlet which is separate module has to check the database continuously, and if the status changes to 1, the servlet is redirected to another screen. Instead of using database as a mediator, can i pass the value from the asterisk java code directly to the servlet code?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
The usual way for Java code to access a web app programmatically would be to use the HttpURLConnection class or a library like Apache HttpClient.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks ulf.I have tried the following code using HTTPURLConnection and opened the connection.But If i need to pass a value to that url,how should i pass the value and how should i retreive the value in the servlet?


manoj r patil
Ranch Hand

Joined: Jun 06, 2002
Posts: 181
pass request parameters in the URL ...


love your job and not your company;
...because you never know when your company will stop loving you!
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks manoj. From your suggestion , I have included the following method in the code as i need to pass value "1" to the variable "calleridstatus"

connection.setRequestProperty("calleridstatus","1");


Is this correct?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
Request properties are not the same thing as request parameters. Example e135 here may help.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
I have tried the following code to pass value 1 to the variable calleridstatus:



Then i tried to retrieve the value in the following jsp code:

I used refresh to refresh the screen just to check if i run the URLConnectionReader.java ,whether the value of the calleridstatus changes to null.But it always displays null.Am i going wrong?

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
I used refresh to refresh the screen just to check if i run the URLConnectionReader.java ,whether the value of the calleridstatus changes to null.But it always displays null.

Of course it's null - a parameter called "calleridstatus" is never sent to the server.

Note that the JSP page claims to be both UTF-8 and ISO-8859-1 - only one of those can be true, so be sure to choose the correct one.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
How should i send the parameter to server?

will the below line does not send parameter to the server?

String data = URLEncoder.encode("calleridstatus", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8");


Thanks
Arundhathi Menon
Ranch Hand

Joined: Jan 14, 2004
Posts: 113
Hi Preethi,

You can try appending the request parameters directtly to the servlet URL -

url ? param1 = value && param2 = value.

In your servlet code , you can retrieve it using request.getParameter("param1").


SCJP , SCWCD , SCBCD , SCDJWS
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
preethi Ayyappan wrote:How should i send the parameter to server?

will the below line does not send parameter to the server?

String data = URLEncoder.encode("calleridstatus", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8");


Thanks

Which part of that line do you think sends anything? Do you know what the URLEncoder.encode method does?
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Urlencoder methods converts string value to MIME format. sorry for misunderstanding the code.

Now, I tried to pass the value in the url directly. eventhough i am unable to get the value.



Then the jsp which retrievex the value is:


Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
That code works fine for me - it prints a "1" to the log file.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Yes.For me also it is working.But i tried to redirect whenever it gets the calleridstatus=1.But when i get the variable and print,the code prints 1.But when i try to redirect to a jsp whenever i get the value 1, I am unable to redirect.the code follows:


Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
I assume by "redirect" you mean "refresh" - which is what that meta-equiv does.

The HttpURLConnection class is not a browser - it does not interpret the content it downloads in any way. If you want to reload the page, you'll have to create a new request.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
I meant redirect to another jsp only.sorry.I have posted the code without redirect yesterday. I tried to redirect to Newjsp.jsp whenever i get the value 1 as follows


In console I can able to view the value as 1 whenever i run the URLConnectionReader.java code.But the page is not redirected to Newjsp.jsp
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
String comparison is not done using the "==" operator, it is done using the "equals" method.

The JSP generates output before the redirect call (yes, white space is output) - at that point a redirect is no longer possible. For this and other reasons (like proper MVC), JSPs are fundamentally wrong for control logic - use a servlet instead that decides to which JSP to forward to.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks for your response. To avoid the control logic in jsp, I tried to get the value of calleridstatus in servlet and if the value is 1, the page is redirected to a jsp.

But if i tried to open the url connection of the servlet(Http://localhost:8080/AVM/HelloServlet), the connection is not opened. The following is servlet code:




Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
But if i tried to open the url connection of the servlet(Http://localhost:8080/AVM/HelloServlet), the connection is not opened.

What do you mean by that? Is there an exception on the client or server side? If so, which one? How are you trying to open the connection? Is the servlet ever accessed, meaning is the doGet method ever executed? The code you posted earlier was using POST, so doGet would not be called.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518


What do you mean by that? Is there an exception on the client or server side?


I meant that I am trying to open the connection using the code "URLConnectionReader.java" as follows:



When i try to open the url which is a jsp(for example:http://localhost:8080/AVM/newlogin.jsp),i can able to see the response code as 200 in the console.But when i try to open the servlet url like"http://localhost:8080/AVM/HelloServlet?calleridstatus=1", the response code is 405.




Previously i have used "Post" method in URLConnectionReader.java. But after that, I have used "Get" method only. where am i wrong here?

Thanks


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
But when i try to open the servlet url like"http://localhost:8080/AVM/HelloServlet?calleridstatus=1", the response code is 405.

Previously i have used "Post" method in URLConnectionReader.java. But after that, I have used "Get" method only. where am i wrong here?

The code is actually doing a POST, not a GET - that's what "setDoOutput(true)" means. Remove that line, and the stuff involving the OutputStreamWriter, and it should work fine.

The response code 405 should actually have provided a clue as to what's going on. The javadocs for HttpServletResponse contain a list of HTTP response codes, and what they mean. The ultimate reference for that is of course the HTTP specification, which you can find in the FAQ specification page linked in my signature.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: core java communicates with servlet or jsp?