-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Goran,

Goran Jambrović wrote:
| I would like to run a servlet(/FunPacmanServlet) from java.

You should call it via HTTP.

| This servlet
| would then show a jsp page in web browser.

That's going to be a problem, depending on your platform, the security
manager under which the JVM is running, and whether or not you know how
to invoke the 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?

Lots of things.

| I am using eclipse
| 3.2, tomcat 5.5,firefox.

What OS?

| public static LoadPortalServlet startServlet = null;

This might be a problem. Let's see...

| private HttpServletRequest request = null;

I smell bad stuff...

| RequestDispatcher dispatcher = request.getRequestDispatcher(jspName);

[snip]

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

Wow. All that code just to forward to another JSP. Why not just discard
the entire class, since it doesn't do anything at all?

| this.request = request;

This is a problem. Not only is it going to confuse the hell out of
everything, but you're violating the servlet specification, which says
that you can't store references to the request anywhere that lives
longer than the request. :(

| this.startServlet = this;

Why bother storing "this" in "this.startServlet". Since "this" is always
"this", you'll never need it. But "startServlet" is static, so you're
storing a reference to the servlet at the Class level. Also bad form:
you're not guaranteed that there is only one instance of your Servlet
class at any given time. How will you know which one you want?

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

This probably isn't what you want, either. Completely ignoring POST
requests? That's just bad manners.

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

Are these two running in the same JVM? If not, then
LoadPortalServlet.startServlet is going to be null every time.

| connection.setRequestMethod("POST");

Ha ha ha. You are calling your servlet with POST, which you are
specifically ignoring. Ugh.

| 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() );

You don't want any of this. Assuming that these values are correct for
the server, they are almost certainly not correct for the client (except
for "host" which will come from the URL).

You should dump all of this. Every single line. That frees you from
having to grab the "startServlet" from a static variable that will never
have a value. It also frees you from having the static variable in the
first place, making your code easier to understand (and probably
removing countless bugs that will crop up in the future).

| String request = "test";

[snip]

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

You don't need a DataOutputStream, here, because you aren't sending
binary data.

| connection.disconnect();

Technically, you should have more robust exception handling, too. But I
don't think this is your current worst problem.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAke9jokACgkQ9CaO5/Lv0PAiFQCgsPPaGQ3qZK+bjT6HerdHTdGH
EYEAnRjw+oTV5t58GSbsy0EfjqyLmErp
=l+fv
-----END PGP SIGNATURE-----

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

Reply via email to