Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

core java communicates with servlet or jsp?

 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Preethi,
Can you elaborate on what you are trying to do? You want to use the java code in the web context?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are these codes running in different JVMs?
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?


 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pass request parameters in the URL ...
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Request properties are not the same thing as request parameters. Example e135 here may help.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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").
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code works fine for me - it prints a "1" to the log file.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Danger, 10,000 volts, very electic .... tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic