是的Java Servlet我在Netbeans中已经创建加一些奇怪的? [英] is the Java Servlet I've created in Netbeans adding something strange?

查看:211
本文介绍了是的Java Servlet我在Netbeans中已经创建加一些奇怪的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第二次​​尝试解决这个问题。我的第一次尝试是<一个href=\"http://stackoverflow.com/questions/1389834/applet-server-communication-how-i-can-do-it\">here但也许我的我的问题的解释是不够的,我的问题是,小程序接收到异常:

  java.io.StreamCorruptedException:无效的流头:0A0A0A3C在
java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)在
java.io.ObjectInputStream中的&LT;&初始化GT;(ObjectInputStream.java:280)

抱歉,如果我听起来像一个破纪录:)

的Java Web - - Web应用程序,并选择GlassFish服务器3根据服务器

我想一个小程序,并在同一台机器上的一个Servlet之间的通信,我已经通过创建一个新的项目创建NetBeans中的servlet。它创建的index.jsp,但我并不真的需要一个网页界面。

我运行的NetBeans(pressing F6)的servlet和它部署在我的浏览器中打开servlet的index.jsp的。我然后运行小程序(从NetBeans中不同的项目),并尝试连接。我仍然收到了良好的醇'无效流头所以我猜故障件事情我已经在做的Netbeans内所在。

我粘贴一些code我以为是工作(老code,但没有发现任何最近的例子满)的code是从公然的链接

那么,到底,喜欢什么我会做的是从servlet发送一个二维数组对象的小程序,当applet请求要发送的数组。在code例子只是说明无效流头我收到。

我觉得/猜小程序receving来自服务器的基于文本的反应,但我想要的回应是一个序列化对象(在code例子只是一个字符串),这将是一个Object [] []以后,如果我得到一个线索。

感谢您的耐心,大师。 :)

小程序code(随时与所有版面code忽略的init()):

 包se.iot.recallapplet;进口java.applet.Applet中;
进口java.awt中的*。
java.awt.event中导入*。
进口java.io. *;
进口java.net *。公共类RecallApplet扩展的Applet {
私人文本字段inputField =新的TextField();
私人文本字段outputField =新的TextField();
私人文本区域exceptionArea =新的TextArea();公共无效的init(){
    //设置新的布局
    的setLayout(新的GridBagLayout());    // 添加标题
    标签标题=新标签(回声小程序,Label.CENTER);
    title.setFont(新字体(SANSSERIF,Font.BOLD,14));
    GridBagConstraints的C =新的GridBagConstraints();
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weightx = 1.0;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets =新插图(5,5,5,5);
    加(标题,C);    //添加输入标签,现场并发送按钮
    C =新的GridBagConstraints();
    c.anchor = GridBagConstraints.EAST;
    添加(新标签(输入,Label.RIGHT),C);
    C =新的GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0;
    加(inputField,C);
    按钮sendButton =新按钮(发送);
    C =新的GridBagConstraints();
    c.gridwidth = GridBagConstraints.REMAINDER;
    加(sendButton,C);
    sendButton.addActionListener(新的ActionListener(){
        公共无效的actionPerformed(ActionEvent的五){
            onSendData();
        }
    });    //添加标签输出和非编辑的字段
    C =新的GridBagConstraints();
    c.anchor = GridBagConstraints.EAST;
    添加(新标签(输出,Label.RIGHT),C);
    C =新的GridBagConstraints();
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0;
    加(outputField,C);
    outputField.setEditable(假);    //添加例外标签和不可编辑的文本区域
    C =新的GridBagConstraints();
    c.anchor = GridBagConstraints.EAST;
    添加(新标签(异常,Label.RIGHT),C);
    C =新的GridBagConstraints();
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    加(exceptionArea,C);
    exceptionArea.setEditable(假);
}/ **
 *获取到servlet的连接。
 * /
私人的URLConnection getServletConnection()
    抛出MalformedURLException的,IOException异常{    //连接的Zum Servlet的ffnen
            网址urlServlet =新的URL(HTTP://本地主机:8080 / Event_Servlet /);
    URLConnection的CON = urlServlet.openConnection();    // konfigurieren
    con.setDoInput(真);
    con.setDoOutput(真);
    con.setUseCaches(假);
    con.setRequestProperty(
        内容类型,
        应用程序/ x-j​​ava的序列化对象);    返回CON;
}/ **
 *在inputField数据发送到servlet和显示结果在outputField。
 * /
私人无效onSendData(){
    尝试{
        //获取输入数据发送
        字符串输入= inputField.getText();        //将数据发送到servlet
        URLConnection的CON = getServletConnection();
        OutputStream的outstream = con.getOutputStream();
        ObjectOutputStream的OOS =新的ObjectOutputStream(outstream);
        oos.writeObject(输入);
        oos.flush();
        oos.close();        //接收来自servlet的结果
        InputStream的INSTR = con.getInputStream();
        ObjectInputStream的inputFromServlet =新的ObjectInputStream(INSTR);
        字符串结果=(字符串)inputFromServlet.readObject();
        inputFromServlet.close();
        instr.close();        //显示结果
        outputField.setText(结果);    }赶上(例外前){
        ex.printStackTrace();
        exceptionArea.setText(ex.toString());
    }
}
}

Servlet的code:

 包se.iot.eventservlet;进口java.io. *;进口javax.servlet.ServletException;
进口javax.servlet.http包*。公共类Event_Servlet延伸的HttpServlet {公共无效的doPost(
    HttpServletRequest的要求,
    HttpServletResponse的响应)
    抛出了ServletException,IOException异常{
    尝试{
        response.setContentType(应用程序/ x-j​​ava的序列化对象);        //读取applet的一个String对象
        //而不是一个String对象,可以传输任何对象,
        //已知到servlet和小应用程序
        InputStream的时间= request.getInputStream();
        ObjectInputStream的inputFromApplet =新的ObjectInputStream(在);
        字符串回声=(字符串)inputFromApplet.readObject();        //它呼应的小程序
        OutputStream的outstr = response.getOutputStream();
        ObjectOutputStream的OOS =新的ObjectOutputStream(outstr);
        oos.writeObject(回声);
        oos.flush();
        oos.close();    }赶上(例外五){
        e.printStackTrace();
    }
}
}

堆栈跟踪:

  java.io.StreamCorruptedException:无效的流头:0A0A0A3C
    在java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
    在java.io.ObjectInputStream中的&LT;&初始化GT;(ObjectInputStream.java:280)
    在se.iot.recallapplet.RecallApplet.onSendData(RecallApplet.java:114)
    在se.iot.recallapplet.RecallApplet.access $ 000(RecallApplet.java:12)
    在se.iot.recallapplet.RecallApplet $ 1.actionPerformed(RecallApplet.java:48)
    在java.awt.Button.processActionEvent(Button.java:392)
    在java.awt.Button.processEvent(Button.java:360)
    在java.awt.Component.dispatchEventImpl(Component.java:4714)
    在java.awt.Component.dispatchEvent(Component.java:4544)
    在java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
    在java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    在java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    在java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    在java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


解决方案

的问题是,小程序无法连接到servlet所以在这里的servlet的code可以忽略不计。

我需要配置的server.xml这个:

 &LT;上下文路径=/ servletName的docBase =servletName调试=0重载=真
   crossContext =真正的&GT;
   &LT; /语境GT;

This is my second try to solve this problem. My first try was here but perhaps my explanation of my problem was insufficient, my problem was that the applet received the exception:

java.io.StreamCorruptedException: invalid stream header: 0A0A0A3C at 
java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783) at  
java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)

sorry if I sound like a broken record :)

I'm trying to communicate between an Applet and a Servlet on the same machine, I've created the servlet in Netbeans by creating a New project - Java Web - Web Application and Choosing Glassfish Server 3 as server. It does create an index.jsp but I don't really need a web page interface.

I run the servlet from NetBeans (pressing f6) and it deploys and opens the servlet's index.jsp in my browser. I then run the applet (from a different project in Netbeans) and try to connect. I still receive the good ol' "invalid stream header" so I'm guessing the fault lies within something I've done in Netbeans.

I pasted some code I assume is working (old code but haven't found any more recent full examples) The code is blatantly stolen from Link

So in the end, what i'd like do is to send a two dimensional Object array from the servlet to the applet when the applet requests the array to be sent. The code examples is just to show the Invalid stream header I'm receiving.

I think/guess the applet is receving a textbased response from the server but I want the response to be a serialized-object (Just a String in the code example), it will be an Object[ ][ ] later, if I ever get a clue.

Thanks for your patience, gurus. :)

Applet code (feel free to ignore init() with all the layout code):

package se.iot.recallapplet;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class RecallApplet extends Applet {
private TextField inputField = new TextField();
private TextField outputField = new TextField();
private TextArea exceptionArea = new TextArea();

public void init() {
    // set new layout
    setLayout(new GridBagLayout());

    // add title
    Label title = new Label("Echo Applet", Label.CENTER);
    title.setFont(new Font("SansSerif", Font.BOLD, 14));
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weightx = 1.0;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(5, 5, 5, 5);
    add(title, c);

    // add input label, field and send button
    c = new GridBagConstraints();
    c.anchor = GridBagConstraints.EAST;
    add(new Label("Input:", Label.RIGHT), c);
    c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0;
    add(inputField, c);
    Button sendButton = new Button("Send");
    c = new GridBagConstraints();
    c.gridwidth = GridBagConstraints.REMAINDER;
    add(sendButton, c);
    sendButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            onSendData();
        }
    });

    // add output label and non-editable field
    c = new GridBagConstraints();
    c.anchor = GridBagConstraints.EAST;
    add(new Label("Output:", Label.RIGHT), c);
    c = new GridBagConstraints();
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0;
    add(outputField, c);
    outputField.setEditable(false);

    // add exception label and non-editable textarea
    c = new GridBagConstraints();
    c.anchor = GridBagConstraints.EAST;
    add(new Label("Exception:", Label.RIGHT), c);
    c = new GridBagConstraints();
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    add(exceptionArea, c);
    exceptionArea.setEditable(false);
}

/**
 * Get a connection to the servlet.
 */
private URLConnection getServletConnection()
    throws MalformedURLException, IOException {

    // Connection zum Servlet ˆffnen
            URL urlServlet = new URL("http://localhost:8080/Event_Servlet/");
    URLConnection con = urlServlet.openConnection();

    // konfigurieren
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false);
    con.setRequestProperty(
        "Content-Type",
        "application/x-java-serialized-object");

    return con;
}

/**
 * Send the inputField data to the servlet and show the result in the outputField.
 */
private void onSendData() {
    try {
        // get input data for sending
        String input = inputField.getText();

        // send data to the servlet
        URLConnection con = getServletConnection();
        OutputStream outstream = con.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(outstream);
        oos.writeObject(input);
        oos.flush();
        oos.close();

        // receive result from servlet
        InputStream instr = con.getInputStream();
        ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
        String result = (String) inputFromServlet.readObject();
        inputFromServlet.close();
        instr.close();

        // show result
        outputField.setText(result);

    } catch (Exception ex) {
        ex.printStackTrace();
        exceptionArea.setText(ex.toString());
    }
}
}

Servlet code:

package se.iot.eventservlet;

import java.io.*;

import javax.servlet.ServletException;
import javax.servlet.http.*;

public class Event_Servlet extends HttpServlet {

public void doPost(
    HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
    try {
        response.setContentType("application/x-java-serialized-object");

        // read a String-object from applet
        // instead of a String-object, you can transmit any object, which
        // is known to the servlet and to the applet
        InputStream in = request.getInputStream();
        ObjectInputStream inputFromApplet = new ObjectInputStream(in);
        String echo = (String) inputFromApplet.readObject();

        // echo it to the applet
        OutputStream outstr = response.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(outstr);
        oos.writeObject(echo);
        oos.flush();
        oos.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

stackTrace:

java.io.StreamCorruptedException: invalid stream header: 0A0A0A3C
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
    at se.iot.recallapplet.RecallApplet.onSendData(RecallApplet.java:114)
    at se.iot.recallapplet.RecallApplet.access$000(RecallApplet.java:12)
    at se.iot.recallapplet.RecallApplet$1.actionPerformed(RecallApplet.java:48)
    at java.awt.Button.processActionEvent(Button.java:392)
    at java.awt.Button.processEvent(Button.java:360)
    at java.awt.Component.dispatchEventImpl(Component.java:4714)
    at java.awt.Component.dispatchEvent(Component.java:4544)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

解决方案

The problem was that the applet couldn't connect to the servlet so the code in the servlet here can be ignored.

I needed to config server.xml with this:

<Context path="/servletName" docBase="servletName" debug="0" reloadable="true"
   crossContext="true">
   </Context>

这篇关于是的Java Servlet我在Netbeans中已经创建加一些奇怪的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆