jason101799
asked on
Need jsp expertise to help...
I am facing a problem with my jsp file. How can I intergrate a jsp file to call a unix shell script??? I needed this because I am using the Unix mailx function to send out mails. So if anyone knows how to invoke this function from jsp file, please help.
Thank you
Cheers
Jason
Thank you
Cheers
Jason
Why don't you use Java Mail 1.2 AI? It's pretty easy to implement, and then you don't have problems trying to trap errors that you will if you call a third party utility.
We used a code sample I got off the Net as a start toward writing our Email utility. I can send you code if you're interested.
Dorothy
We used a code sample I got off the Net as a start toward writing our Email utility. I can send you code if you're interested.
Dorothy
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Naughton,
Do I have to write this codes in java??? Can I just put all ya codes in a JSP file instead?? Sorry I am new to Java and JSP cause I am use to ASP syntax. Mind if you can briefly explain your proposed answer.
Thank you.
Cheers
Jason
Do I have to write this codes in java??? Can I just put all ya codes in a JSP file instead?? Sorry I am new to Java and JSP cause I am use to ASP syntax. Mind if you can briefly explain your proposed answer.
Thank you.
Cheers
Jason
Jason,
The code that is within the body of the sendMail( ) method can be added directly to your JSP. Just make sure you place the code within a set of <% %> tags and declare the variables before you use them. I don't think you need to import java.io.* and java.net.* in the JSP since they are used by the JSP anyway. Before you write any code, I do recommend using telnet to connect to your mail server on port 25 and hand type the SMTP commands so you can see exactly how your mail sever responds. The socket connection you establish in your JSP just automates what you will do manually with telnet.
You should also get the input stream from the socket so you can check the response from the mail server after each SMTP command. You can do this with something like:
BufferedReader is = new BufferedReader( new inputStreamReader( socket.getInputStream() ) );
Then call is.readln() after each ps.println( ) to capture the server's response. A response of 250 means everything is OK. In general any response starting with a 2 or a 3 is positive, anything else is some sort of error. You can check the RFC for the meaning of all the response codes.
Good Luck!
John
The code that is within the body of the sendMail( ) method can be added directly to your JSP. Just make sure you place the code within a set of <% %> tags and declare the variables before you use them. I don't think you need to import java.io.* and java.net.* in the JSP since they are used by the JSP anyway. Before you write any code, I do recommend using telnet to connect to your mail server on port 25 and hand type the SMTP commands so you can see exactly how your mail sever responds. The socket connection you establish in your JSP just automates what you will do manually with telnet.
You should also get the input stream from the socket so you can check the response from the mail server after each SMTP command. You can do this with something like:
BufferedReader is = new BufferedReader( new inputStreamReader( socket.getInputStream() ) );
Then call is.readln() after each ps.println( ) to capture the server's response. A response of 250 means everything is OK. In general any response starting with a 2 or a 3 is positive, anything else is some sort of error. You can check the RFC for the meaning of all the response codes.
Good Luck!
John
ASKER
John,
How do we know what type of page import we need when we copy the source from java to jsp?
Cheers
Jason
How do we know what type of page import we need when we copy the source from java to jsp?
Cheers
Jason
Jason,
The following line will include the two packages required for the mail example. Just add it to the top of your JSP.
<%@ page import="java.io.*, java.net.* %>
John
The following line will include the two packages required for the mail example. Just add it to the top of your JSP.
<%@ page import="java.io.*, java.net.* %>
John
ASKER
John,
Thanks for the useful information. Before I award you the points, I would like to seek your expertise in JSP. Actually how do I call a function in JSP?
Perhaps you can go thru my codes and brief me.
-------------------------- ---------- ---------- ---------- ---
<%@ page language="java" %>
<%@ page import="java.util.*, java.io.*, java.net.*" %>
<%! public String getCountries() {
String addText="";
String cntry_cd="";
String cntry_nm="";
Class.forName("com.sun.jdb c.driver") ;
String url= "jdbc:sqli://123.256.456:1 234/test";
Connection conn = DriverManager.getConnectio n(url,"tes t","test") ;
DatabaseMetaData meta = conn.getMetaData();
Statement stmt = conn.createStatement();
String sql = "select * from country";
ResultSet rslt = stmt.executeQuery(sql);
while ( rslt.next() )
{
cntry_cd = rslt.getString("ctry_cd");
cntry_nm = rslt.getString("ctry_nm");
addText = addText + "<OPTION VALUE=\"" + cntry_cd + "\">" + cntry_nm + "</OPTION>\n";
}
stmt.close();
conn.close();
return addText;
}
%>
<HTML>
<HEAD>
<TITLE>Commodity Page</TITLE>
</HEAD> <BODY>
<center> <IMG SRC=/images/wsisupport/web shipping.g if
height=100>
<HR> </center>
<FORM NAME="login" ACTION="test.jsp" METHOD="post">
<TABLE BORDER=0 CELLSPACING=4 CELLPADDING=3 WIDTH="100%">
<TR>
<TH>Commodity Search</TH>
</TR>
<TR> <TD>Select Country</TD>
<TD><SELECT NAME="country"><%= getCountries() %></SELECT></TD>
</TR></TABLE>
</BODY>
</HTML>
-------------------------- ---------- ---------- ---------- ---
Thank you.
Cheers
Jason
Thanks for the useful information. Before I award you the points, I would like to seek your expertise in JSP. Actually how do I call a function in JSP?
Perhaps you can go thru my codes and brief me.
--------------------------
<%@ page language="java" %>
<%@ page import="java.util.*, java.io.*, java.net.*" %>
<%! public String getCountries() {
String addText="";
String cntry_cd="";
String cntry_nm="";
Class.forName("com.sun.jdb
String url= "jdbc:sqli://123.256.456:1
Connection conn = DriverManager.getConnectio
DatabaseMetaData meta = conn.getMetaData();
Statement stmt = conn.createStatement();
String sql = "select * from country";
ResultSet rslt = stmt.executeQuery(sql);
while ( rslt.next() )
{
cntry_cd = rslt.getString("ctry_cd");
cntry_nm = rslt.getString("ctry_nm");
addText = addText + "<OPTION VALUE=\"" + cntry_cd + "\">" + cntry_nm + "</OPTION>\n";
}
stmt.close();
conn.close();
return addText;
}
%>
<HTML>
<HEAD>
<TITLE>Commodity Page</TITLE>
</HEAD> <BODY>
<center> <IMG SRC=/images/wsisupport/web
height=100>
<HR> </center>
<FORM NAME="login" ACTION="test.jsp" METHOD="post">
<TABLE BORDER=0 CELLSPACING=4 CELLPADDING=3 WIDTH="100%">
<TR>
<TH>Commodity Search</TH>
</TR>
<TR> <TD>Select Country</TD>
<TD><SELECT NAME="country"><%= getCountries() %></SELECT></TD>
</TR></TABLE>
</BODY>
</HTML>
--------------------------
Thank you.
Cheers
Jason
ASKER
John,
Thanks for the useful information. Before I award you the points, I would like to seek your expertise in JSP. Actually how do I call a function in JSP?
Perhaps you can go thru my codes and brief me.
-------------------------- ---------- ---------- ---------- ---
<%@ page language="java" %>
<%@ page import="java.util.*, java.io.*, java.net.*" %>
<%! public String getCountries() {
String addText="";
String cntry_cd="";
String cntry_nm="";
Class.forName("com.sun.jdb c.driver") ;
String url= "jdbc:sqli://123.256.456:1 234/test";
Connection conn = DriverManager.getConnectio n(url,"tes t","test") ;
DatabaseMetaData meta = conn.getMetaData();
Statement stmt = conn.createStatement();
String sql = "select * from country";
ResultSet rslt = stmt.executeQuery(sql);
while ( rslt.next() )
{
cntry_cd = rslt.getString("ctry_cd");
cntry_nm = rslt.getString("ctry_nm");
addText = addText + "<OPTION VALUE=\"" + cntry_cd + "\">" + cntry_nm + "</OPTION>\n";
}
stmt.close();
conn.close();
return addText;
}
%>
<HTML>
<HEAD>
<TITLE>Commodity Page</TITLE>
</HEAD> <BODY>
<center> <IMG SRC=/images/wsisupport/web shipping.g if
height=100>
<HR> </center>
<FORM NAME="login" ACTION="test.jsp" METHOD="post">
<TABLE BORDER=0 CELLSPACING=4 CELLPADDING=3 WIDTH="100%">
<TR>
<TH>Commodity Search</TH>
</TR>
<TR> <TD>Select Country</TD>
<TD><SELECT NAME="country"><%= getCountries() %></SELECT></TD>
</TR></TABLE>
</BODY>
</HTML>
-------------------------- ---------- ---------- ---------- ---
Thank you.
Cheers
Jason
Thanks for the useful information. Before I award you the points, I would like to seek your expertise in JSP. Actually how do I call a function in JSP?
Perhaps you can go thru my codes and brief me.
--------------------------
<%@ page language="java" %>
<%@ page import="java.util.*, java.io.*, java.net.*" %>
<%! public String getCountries() {
String addText="";
String cntry_cd="";
String cntry_nm="";
Class.forName("com.sun.jdb
String url= "jdbc:sqli://123.256.456:1
Connection conn = DriverManager.getConnectio
DatabaseMetaData meta = conn.getMetaData();
Statement stmt = conn.createStatement();
String sql = "select * from country";
ResultSet rslt = stmt.executeQuery(sql);
while ( rslt.next() )
{
cntry_cd = rslt.getString("ctry_cd");
cntry_nm = rslt.getString("ctry_nm");
addText = addText + "<OPTION VALUE=\"" + cntry_cd + "\">" + cntry_nm + "</OPTION>\n";
}
stmt.close();
conn.close();
return addText;
}
%>
<HTML>
<HEAD>
<TITLE>Commodity Page</TITLE>
</HEAD> <BODY>
<center> <IMG SRC=/images/wsisupport/web
height=100>
<HR> </center>
<FORM NAME="login" ACTION="test.jsp" METHOD="post">
<TABLE BORDER=0 CELLSPACING=4 CELLPADDING=3 WIDTH="100%">
<TR>
<TH>Commodity Search</TH>
</TR>
<TR> <TD>Select Country</TD>
<TD><SELECT NAME="country"><%= getCountries() %></SELECT></TD>
</TR></TABLE>
</BODY>
</HTML>
--------------------------
Thank you.
Cheers
Jason
Jason,
Your code looks fine. You correctly defined your getCountries( ) method within the <%! %> tag. Are you getting an error? You could try changing your getCountries( ) method to return a simple string just to see if getCountries( ) is getting called correctly. If your simplified method works then it may be a problem with your database connection.
<%! public String getCountries()
{ return " Get Countries Worked!";
} %>
John
p.s. Are two questions worth twice as many points?
Your code looks fine. You correctly defined your getCountries( ) method within the <%! %> tag. Are you getting an error? You could try changing your getCountries( ) method to return a simple string just to see if getCountries( ) is getting called correctly. If your simplified method works then it may be a problem with your database connection.
<%! public String getCountries()
{ return " Get Countries Worked!";
} %>
John
p.s. Are two questions worth twice as many points?
ASKER
John,
If I get my answer, it's worth twice.:)
Anyway u should get your points for the mail question posted. Perhaps you can help me with the second qs. How do I display my results in pages?? It would be nice if you provide me some advice and source codes.
Cheers
Jason
If I get my answer, it's worth twice.:)
Anyway u should get your points for the mail question posted. Perhaps you can help me with the second qs. How do I display my results in pages?? It would be nice if you provide me some advice and source codes.
Cheers
Jason
Jason,
It does not look like you have a closing form tag </form>. This could be why you do not see the list on your page. Any elements that belong on a form, such as a selection list, will not be displayed if they are not surrounded by the form tags.
John
It does not look like you have a closing form tag </form>. This could be why you do not see the list on your page. Any elements that belong on a form, such as a selection list, will not be displayed if they are not surrounded by the form tags.
John
ASKER
Thanks John,
Have a nice day ahead.
Cheers
Jason
Have a nice day ahead.
Cheers
Jason
to call ur Shell Script.
But shell script should be present the location of http
Use
Runtime.exec("shell.sh");
From your program
Shyam