Hi servlet gurus,
Env:
HP-UX 10.20, Java 1.17, JSDK 2.1
There's a small servlet I've written, that calls the UNIX "uptime" and "w"
commands using a Process object, reads their output and displays it
back to the user. It gives the error shown below at the printW() method
(blank/irrelevant lines deleted from program and output). You have
to run the servlet, check the w checkbox, then click the submit button
to generate the error. Any ideas ? There is no indication of which
line the error is occurring on, though an earlier bug (also a NPE) was fixed
after seeing the line no. in the error msg.
TIA
Vasudev
Host : atlas
...
w output:
Error: 500
Internal Servlet Error:
java.lang.NullPointerException
at SrvStat.printW(Compiled Code)
at SrvStat.doGet(Compiled Code)
at SrvStat.doPost(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at com.sun.web.core.ServletWrapper.handleRequest(Compiled Code)
at com.sun.web.core.InvokerServlet.service(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at com.sun.web.core.ServletWrapper.handleRequest(Compiled Code)
at com.sun.web.core.Context.handleRequest(Compiled Code)
at com.sun.web.server.ConnectionHandler.run(Compiled Code)
The program :
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SrvStat extends HttpServlet {
PrintWriter srvOut; // servlet output stream
PrintWriter prcOut; // output stream to process input
PrintWriter logOut; // output stream for log file for program
File logFile; // log file for program
BufferedReader prcIn; // input stream from process output
Runtime runtime; // to start the process
Process process; // a process
String command; // command to run and write/read to/from
String result;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
logFile = new File("/tmp/SrvStat.log");
logOut = new PrintWriter( new FileWriter(logFile), true);
logOut.println("Opened log file");
response.setContentType("text/html");
PrintWriter srvOut = response.getWriter();
logOut.println("Before getRuntime");
runtime = Runtime.getRuntime();
logOut.println("Before <html>");
srvOut.println("<html>");
srvOut.println("<body bgcolor=\"white\">");
srvOut.println("<head>");
String title = "Server Status";
srvOut.println("<title>" + title + "</title>");
srvOut.println("</head>");
srvOut.println("<body>");
logOut.println("Before calling getHostName");
srvOut.println("<h2>Host : " + getHostName() + "</h2>");
srvOut.println("<br><hr>");
HttpSession session = request.getSession();
srvOut.println("id" + " " + session.getId());
srvOut.println("<br>");
srvOut.println("created" + " ");
srvOut.println(new Date(session.getCreationTime()) + "<br>");
srvOut.println("lastaccessed" + " ");
srvOut.println(new Date(session.getLastAccessedTime()));
srvOut.println("<P><hr>");
String uptimeOn = request.getParameter("uptime");
logOut.println("uptimeOn = " + uptimeOn);
if (uptimeOn != null) {
if (uptimeOn.equals("on")) {
logOut.println("Before calling getUptime");
srvOut.println("uptime output: <br>" +
getUptime() + "<br><hr>");
}
}
String wOn = request.getParameter("w");
logOut.println("wOn = " + wOn);
if (wOn != null) {
if (wOn.equals("on")) {
logOut.println("Before calling printW");
srvOut.println("w output: <br>");
printW();
srvOut.println("<br><hr>");
}
}
srvOut.println("<P>");
srvOut.print("<form action=\"");
srvOut.print("SrvStat\" ");
//srvOut.println("method=POST>");
srvOut.println("method=GET>");
srvOut.println("uptime");
srvOut.println("<input type=checkbox name=uptime>");
srvOut.println("<br>");
srvOut.println("w");
srvOut.println("<input type=checkbox name=w>");
srvOut.println("<br>");
srvOut.println("<input type=submit>");
srvOut.println("</form>");
srvOut.println("</body>");
srvOut.println("</html>");
logOut.println("Before leaving doGet");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
logOut.println("Entered doPost");
logOut.println("Calling doGet");
doGet(request, response);
logOut.println("Leaving doPost");
}
String getHostName() throws IOException {
logOut.println("Entered getHostName");
try {
command = "hostname";
process = runtime.exec(command);
prcOut = new PrintWriter( new OutputStreamWriter (
process.getOutputStream() ) );
prcIn = new BufferedReader( new InputStreamReader (
process.getInputStream() ) );
result = prcIn.readLine();
}
catch(Exception e) {
result = "Error executing " + command +
": " + e.getMessage();
}
logOut.println("Leaving getHostName");
return result;
}
String getUptime() throws IOException {
logOut.println("Entered getUptime");
try {
command = "uptime";
process = runtime.exec(command);
prcOut = new PrintWriter( new OutputStreamWriter (
process.getOutputStream() ) );
prcIn = new BufferedReader( new InputStreamReader (
process.getInputStream() ) );
result = prcIn.readLine();
}
catch(Exception e) {
result = "Error executing " + command +
": " + e.getMessage();
}
finally {
prcOut.close();
prcIn.close();
logOut.println("Leaving getUptime");
return result;
}
}
void printW() throws IOException {
logOut.println("Entered printW");
try {
command = "w";
process = runtime.exec(command);
prcOut = new PrintWriter( new OutputStreamWriter (
process.getOutputStream() ) );
prcIn = new BufferedReader( new InputStreamReader (
process.getInputStream() ) );
result = "";
String line = "";
while ((line = prcIn.readLine()) != null) {
srvOut.println("<br>" + line);
}
}
catch(Exception e) {
srvOut.println("Error executing " + command +
": " + e.getMessage());
}
finally {
prcOut.close();
prcIn.close();
logOut.println("Leaving printW");
}
}
}
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html