Servlet Problem

CosmoKey

Mitglied
Hallo zusammen!

Ich hab da noch so ein Problem mit der Anzeig von HTML aus einem Servlet herraus.

Das ist mein Code
PHP:
package de.cx30.balancer.test;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import de.cx30.balancer.utils.list.*;

public class Redirect extends HttpServlet {

	public Redirect() {
	}

	public void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException,
			IOException {
		processRequest(request, response);
	}

	public void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException,
			IOException {
		processRequest(request, response);
	}

	
	private void processRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		System.out.println(session.getId());
		String requestedUri = request.getRequestURI();
		
		String webapp = getWebAppNameFromURI(requestedUri);//parseURI(requestedUri);  
		
		String s = webapp;//getSite(webapp);
		String s4 = "";
		
		OutputStream streamOut = null;
		
		try {
			for (Enumeration enumeration = request
					.getParameterNames(); enumeration.hasMoreElements();) {
				String s2 = (String) enumeration.nextElement();
				if (s4.length() > 0)
					s4 = s4 + "&";
				s4 = s4
						+ s2
						+ "="
						+ URLEncoder
								.encode(request.getParameter(s2));
			}

			//requestedUri += "?" + s4;
			URL url = getSite(webapp, requestedUri);//new URL(s);
			HttpURLConnection urlconnection = (HttpURLConnection) url.openConnection();
			
			urlconnection.setDoInput(true);
			urlconnection.setDoOutput(true);
			urlconnection.setUseCaches(false);
			urlconnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			
			for (Enumeration enumeration1 = request.getHeaderNames(); enumeration1.hasMoreElements();) {
				String s3 = (String) enumeration1.nextElement();
				if (!s3.equals("Content-Type")) {
					if (s3.indexOf("host") == -1) {
						urlconnection.setRequestProperty(s3, request.getHeader(s3));
					}
				}
			}

			Cookie acookie[] = request.getCookies();
			if (acookie != null) {
				for (int i = 1; i < acookie.length; i++)
					urlconnection.setRequestProperty("Set-Cookie" + i,
							acookie[i - 1].getName() + "="
									+ acookie[i - 1].getValue());

			}

			 DataOutputStream dataoutputstream = new DataOutputStream(urlconnection.getOutputStream());
			 dataoutputstream.writeBytes(s4);
			 dataoutputstream.flush();
			 dataoutputstream.close();

			
			InputStream inputstream = null;
			if (urlconnection.getInputStream() != null) {
				inputstream = urlconnection.getInputStream();
			} else if (urlconnection.getErrorStream() != null) {
				inputstream = urlconnection.getErrorStream();
			}
			if (inputstream != null) {
				String contentType = urlconnection.getContentType();
				response.setContentType(contentType);
				String encoding = urlconnection.getContentEncoding();
				response.setCharacterEncoding("utf-8");
				streamOut = response.getOutputStream();
				rewriteStreams(inputstream, streamOut);
				inputstream.close();
			} else {
				writeError(response, s, "", streamOut);
			}
		} catch (Exception e) {
			e.printStackTrace();
			writeError(response, s, "", streamOut);
		} finally {
			if (streamOut != null) {
				streamOut.close();
			}
		}
	}
	
	private String getWebAppNameFromURI (String uri) {
		if (uri.startsWith("/")) {
			uri = uri.substring(1, uri.length());
		}
		if (uri.indexOf("/") != -1) {
			uri = uri.substring(0, uri.indexOf("/"));
		}
		return uri;
	}

	private void writeError(HttpServletResponse response, String host, String message, OutputStream streamOut) {
		try {
			response.setContentType("text/html");
			if (streamOut == null) {
				streamOut = response.getOutputStream();
			}
			streamOut.write("<html>".getBytes());
			streamOut.write("<head>".getBytes());
			streamOut.write("<title>Error message</title>".getBytes());
			streamOut.write("</head>".getBytes());
			streamOut.write("<body>".getBytes());
			String error = "<h>" + message + "</h1>";
			streamOut.write(error.getBytes());
			streamOut.write("<br><br>".getBytes());
			error = "<font size=+1 face=\"Arial\">Could not connect to site " + host + "</font><br>";
			streamOut.write(error.getBytes());
			streamOut.write("</body>".getBytes());
			streamOut.write("</html>".getBytes());
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private URL getSite(String webapp, String path) {
		Server server = WebAppList.getNextServer(webapp);
		
		URL tempurl= null;
		try {
			tempurl = new URL("http", server.getHostName(), server.getServerPort(), path); //"/" +
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
		return tempurl;
	}

	private void rewriteStreams(InputStream inputstream,
			OutputStream outputstream) throws IOException {
		byte abyte0[] = new byte[8192];
		int i;
		//StringBuffer buffer = new StringBuffer();
		while ((i = inputstream.read(abyte0)) > 0) {
			//buffer.append(new String(abyte0));
			outputstream.write(abyte0);
		}
		//System.out.println(buffer.toString());
		//outputstream.write(buffer.toString().getBytes());
	}
}

Kurzee Beschreibung:
Eine Client schickt eine anfrage an das Servlet. Das Servlet schickt die Anfrage an einen Server und die leitet die anwtort wieder zurück an den Client.

Aber irgendwie kommt nur müll zurück. Das HTML scheint, auf dem Weg irgendwie kaput zu gehen. Kann aber auch gut sein, dass irgendwo ein Encoding oder so was nicht richtig gesetzt ist.

Ich hoffe, jemand hier kann mir weiterhelfen.


Besten dank.
 
konnte das Problem jetzt lösen:


Es lag an der funktion rewriteStreams(), irgendwie ist die nicht richtig klar gekommen mit dem byte array.


ich habe sie umgebaut, jetzt es.
PHP:
private void rewriteStreams(InputStream inputstream,
			OutputStream outputstream) throws IOException {
		int i;
		while ((i = inputstream.read()) > -1) {
			outputstream.write(new Integer(i).byteValue());
		}
	}
 

Neue Beiträge

Zurück