tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Goran Jambrović <goran.jambrov...@uni-mb.si>
Subject running servlet from java
Date Wed, 20 Feb 2008 12:15:06 GMT
Hi!

I would like to run a servlet(/FunPacmanServlet) from java.This servlet 
would then show a jsp page in web browser. The problem is that the 
contents of jsp page is shown in eclipse console, but the browser does 
not show the page. Any idea what i'm doing wrong? I am using eclipse 
3.2, tomcat 5.5,firefox. Does tomcat maybe need some setting up?

Best regards, Goran


***************************My main servlet 
code:****************************

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import org.umb.portal.app.unimod.*;
import com.evelopers.unimod.*;
import java.util.Enumeration;

/**
* Servlet implementation class for Servlet: LoadPortalServlet
*
*/
public class LoadPortalServlet extends javax.servlet.http.HttpServlet 
implements javax.servlet.Servlet {

BqPortalEngine fsmEngine = new BqPortalEngine();

public static LoadPortalServlet startServlet = null;

private HttpServletRequest request = null;
private String jspName = "main/portal.jsp";
private String userAgent = "Mozilla 5.0";
private String contentType = "application/x-www-form-urlencoded";
private String accept = 
"application/x-shockwave-flash,text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";


private String keepAlive = "300";
private String host = "localhost:8080";
private String contentLength = "-1";
private String acceptLanguage= "sl,en-gb;q=0.7,en;q=0.3";
private String acceptEncoding= "gzip,deflate";
private String acceptCharset = "ISO-8859-2,utf-8;q=0.7,*;q=0.7";

public HttpServletRequest getRequest() { return request; }
public String getUserAgent() { return userAgent; }
public String getContentType() { return contentType; }
public String getAccept() { return accept; }
public String getKeepAlive() { return keepAlive; }
public String getHost() { return host; }
public String getContentLength() { return contentLength; }
public String getAcceptLanguage() { return acceptLanguage;}
public String getAcceptEncoding() { return acceptEncoding;}
public String getAcceptCharset() { return acceptCharset; }

public LoadPortalServlet() {
super();
}

public void init(){
System.out.println("START");

System.out.println("STOP");
}

public void doGet(HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException
{
// System.out.println("GLEJ:"+fsmEngine.getCounter());
// fsmEngine.fireEvent();

RequestDispatcher dispatcher = request.getRequestDispatcher(jspName);

System.out.println("#### Request MAIN portal servlet #####\n"
+ request.getRequestURI() + "\n"
+ request.getServletPath() + "\n"
+ request.getServerPort() + "\n"
+ request.getRemoteHost() + "\n"
+ request.getAuthType() + "\n"
+ request.getCharacterEncoding() + "\n"
+ request.getContentLength() + "\n"
+ request.getContentType() + "\n"
+ request.getContextPath() + "\n"
+ request.getLocalAddr() + "\n"
+ request.getLocalName()+ "\n"
+ request.getLocalPort()+ "\n"
+ request.getMethod()+ "\n"
+ request.getPathInfo()+ "\n"
+ request.getPathTranslated()+ "\n"
+ request.getProtocol()+ "\n"
+ request.getQueryString()+ "\n"
+ request.getUserPrincipal()+ "\n"
+ request.getSession()+ "\n"
+ request.getRequestURL()+ "\n"
+ request.getParameter("url") +"\n" );

for (Enumeration t=request.getHeaderNames(); t.hasMoreElements();)
{
String headerName = (String) t.nextElement();
System.out.println ( "> #" + headerName + "#" + request.getHeader( 
headerName ) +"#\n" );
}
System.out.println( "\n#### END ####\n" );

this.request = request;
this.startServlet = this;

userAgent = request.getHeader("User-Agent");
accept = request.getHeader("accept");
keepAlive = request.getHeader("keep-alive");
host = request.getHeader("host");
contentLength = request.getHeader("content-length");
acceptLanguage = request.getHeader("accept-language");
acceptEncoding = request.getHeader("accept-encoding");
acceptCharset = request.getHeader("accept-charset");
contentType = 
"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";



if (dispatcher != null)
{
dispatcher.forward(request, response);
}

}

protected void doPost(HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException {
doGet(request, response);
}
}


***********************My java code (only the function & 
imports):*********************

import java.io.IOException;
import com.evelopers.common.exception.SystemException;
import com.evelopers.unimod.runtime.context.StateMachineContext;
import com.evelopers.unimod.runtime.ControlledObject;
import com.evelopers.unimod.core.stateworks.Event;
import com.evelopers.unimod.runtime.*;
import com.evelopers.unimod.runtime.context.*;
import com.evelopers.common.exception.CommonException;
import com.evelopers.unimod.transform.xml.*;
import org.umb.portal.app.config.*;
import org.umb.portal.web.LoadPortalServlet;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Date;
import java.util.ArrayList;
import java.net.URL;
import java.net.URLConnection;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

public void LoadWebPage ( StateMachineContext context )
{
LoadPortalServlet mainServlet = LoadPortalServlet.startServlet;

if ( mainServlet == null )
{
System.out.println ( "Main servlet not loaded yet ... ");
return;
}

String a = "http://localhost:8080/Bq-Portal/FunPacmanServlet";

System.out.println("# Loading servlet ..... " + a);

java.net.URL url = null;
HttpURLConnection connection = null;

try
{
url = new java.net.URL(a);
}
catch (MalformedURLException e)
{
System.out.println("#ERROR:" + e.toString());
}

try
{
connection = (HttpURLConnection) url.openConnection();
}
catch(IOException e)
{
System.out.println("#ERROR:" + e.toString());
}

try
{

String request = "test";

connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);

connection.setRequestProperty("host", mainServlet.getHost());
connection.setRequestProperty("accept", mainServlet.getAccept());
connection.setRequestProperty("accept-language",mainServlet.getAcceptLanguage()); 

connection.setRequestProperty("accept-encoding",mainServlet.getAcceptEncoding()); 

connection.setRequestProperty("accept-charset", 
mainServlet.getAcceptCharset());
connection.setRequestProperty("user-agent", mainServlet.getUserAgent() );
connection.setRequestProperty("keep-alive", mainServlet.getKeepAlive() );
connection.setRequestProperty("content-length", 
mainServlet.getContentLength() );
connection.setRequestProperty("content-type", 
mainServlet.getContentType() );

connection.setAllowUserInteraction(true);

DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(request);
out.flush();
out.close();

BufferedReader reader = new BufferedReader ( new InputStreamReader ( 
connection.getInputStream()));
String response = reader.readLine();
while(null != response)
{
System.out.println(response);
response = reader.readLine();
}

}
catch (IOException e)
{
System.out.println("#Exception: " + e.toString());
}

System.out.println ( "SET URL HOST " + url.getHost());

connection.disconnect();


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message