Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help Needed For JSP code

 
Upendra Bhole
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Sir/Madam

The code you have given in that the ip to be ping is hard coded.But actually I want to make it as variable,
See the following code and suggect me in the second jsp what i have to change.


First I made the table name pingip

1)
select * from pingip
atmid name atmip routerip
------- ------- ------- ---------
S1AC0635 abc 198.24.37.7 198.24.37.7
S1AN0631 abc1 187.25.16.2 187.25.16.2

2)
<HTML>
<HEAD>
<center>
<H1>Enter The Station ID<H1>
</HEAD>
<BODY>
<form action="http://localhost:8100/pingip.jsp">
<table border =1 cellpadding=0>
<TR>
<TD><H4>ATM ID :</H4><input type="text" name ="a"></TD>

</TR><BR></table><br>
<INPUT TYPE="submit" value="Ping ATMIP" STYLE="COLOR:BLUE" align = centre>
</center>
</form>
</BODY>
</HTML>

3)

<%@page import="java.io.*"%>
<%@page import="java.sql.*"%>
<%@page import="java.lang.*"%>
<%
String aip = request.getParameter("a");
response.setContentType("text/plain");
try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc.odbc:ash","","");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select atmip from pingip where atmid='"+aip+"'");
Runtime r = Runtime.getRuntime();
Process p = r.exec("ping ");
InputStream ins =p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
String s = br.readLine();
while(s != null)
{
out.println(s);
System.out.println(s);
s = br.readLine();
}

con.close();
}//end of try



catch(SQLException eb)
{
out.println(eb.getMessage());
}//end of catch
catch(Exception ea)
{
out.println(ea.getMessage());
}//end of catch


%>


---------

From First jsp I will put in the textbox atm id S1AC0635.Now if i want to ping ip 198.24.37.7 what could be the change in my sencond jsp especially in

Process p = r.exec("ping ");
So that I can ping any ip from database table "pingip"
Please suggest me.

Upendra
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pass it into the page as a variable, and concatenate that variable to the String you pass to the exec method:




This would apply if your JSP page is called MyPage.jsp, so you would type the following into your browser:

MyPage.jsp?address=192.144.3.1

to ping 192.144.3.1
 
Upendra Bhole
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.
The code which you have given in that you are passing the ip in text box and that ip you are accessing in second page and inserting into the exec method.

But I am not passing ip in text box in first page.
My doubt is that how i will pass the out put of query ResultSet into the
exec method.ie whatever the value stored inthe ResultSet object rs that value I want to pass into exec method.Below logic I have applied but it is not working.Please suggect me with code.
.
.
.
ResultSet rs = st.executeQuery("select atmip from pingip where atmid='"+aip+"'");
Runtime r = Runtime.getRuntime();
Process p = r.exec("ping" +rs);
.
.


Regards
Upendra
 
hassan mahmood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is the correct code........

ResultSet rs = st.executeQuery("select atmip from pingip where atmid='"+aip+"'");
Runtime r = Runtime.getRuntime();
rs.next();
String ip = rs.getString(0);
Process p = r.exec("ping " +ip);


Hassan
SCJP2
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whatever. Its a bad idea to write JDBC code inside JSP. JSP is meant for presention though.
 
Upendra Bhole
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your suggestion.
I have tried for the same code as you suggest me.
It is giving the error as below.

[Microsoft][ODBC Driver Manager] Invalid descriptor index


Upendra
 
Upendra Bhole
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any body help me.
Please

Upendra
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Upendra,

I think for getting ip you should try it as



in any case do remember what adeel said...

presentation layer should not contain any JDBC or code related to database.
It is not considered as good practice

thanks
Shailesh
 
Upendra Bhole
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thaks for your reply.
But when I insert the s1ac0635 in text box then i tried.

.
.
String ip = rs.getString(1);
.
.

it gives out put in browser as below

create process: ping198.24.37.7 error=2

Instead of pinging characteristics...

Please

Upendra
 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Upendra Bhole:


it gives out put in browser as below

create process: ping198.24.37.7 error=2

Instead of pinging characteristics...

Please

Upendra



Look at the code



leave a whitespace between " & ping like "ping "

hth
-MB
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic