求助:在做:J2ME 实现简单电子邮件发送功能 时,遇到的问题!
执行:http://localhost:8888/cs/MaileServlet时,出现错误如下:
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.io.EOFException
java.io.DataInputStream.readUnsignedShort(DataInputStream.java:310)
java.io.DataInputStream.readUTF(DataInputStream.java:545)
java.io.DataInputStream.readUTF(DataInputStream.java:522)
servlet.MaileServlet.doPost(MaileServlet.java:60)
servlet.MaileServlet.doGet(MaileServlet.java:51)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
请问各位大侠哪里错了?该如何解决呢?
代码如下:
--------------MIDlet-----------
package com;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MailClient extends MIDlet {
private MainForm mainForm;
private ContentForm contentForm;
private Display display;
private Message message;
public Message getMessage()
{
return message;
}
public void setMessage(Message message)
{
this.message = message;
}
public void displayAlert(String text, AlertType type, Displayable disp)
{
Alert alert = new Alert( "Application Error ");
alert.setString(text);
alert.setType(type);
alert.setTimeout(2000);
display.setCurrent(alert, disp);
}
public ContentForm getContentForm()
{
return contentForm;
}
public Display getDisplay()
{
return display;
}
public MainForm getMainForm()
{
return mainForm;
}
public void initMIDlet()
{
MailThread t = new MailThread(this);
t.start();
message = new Message();
display = Display.getDisplay(this);
mainForm = new MainForm(this, "Simple Mail Client ");
contentForm = new ContentForm( "Content ", null, 150, TextField.ANY, this);
display.setCurrent(mainForm);
}
public MailClient() {
// TODO Auto-generated constructor stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
initMIDlet();
}
class MailThread extends Thread
{
private MailClient midlet;
public MailThread(MailClient midlet)
{
this.midlet = midlet;
}
public void run()
{
synchronized(midlet)
{
try
{
midlet.wait();
}
catch(Exception e)
{
e.printStackTrace();
}
}
System.out.println( "connecting to server..... ");
HttpConnection httpConn = null;
DataOutputStream dos = null;
try
{
httpConn =
(HttpConnection)Connector.open( "http://localhost:8888/cs/MaileServlet ");
httpConn.setRequestMethod( "POST ");
dos = new DataOutputStream(httpConn.openOutputStream());
dos.writeUTF(midlet.getMessage().getTo());
dos.writeUTF(midlet.getMessage().getSubject());
dos.writeUTF(midlet.getMessage().getContent());
dos.close();
httpConn.close();
System.out.println( "end of sending mail ");
}
catch(IOException e)
{}
}
}
}
------------servlet--------------------
package servlet;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MaileServlet extends HttpServlet {
private static String host;
private static String from;
public void init(ServletConfig config) throws ServletException {
// Put your code here
super.init(config);
host = config.getInitParameter( "host ");
from = config.getInitParameter( "from ");
System.out.println(host + from);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DataInputStream dis = new DataInputStream(request.getInputStream());
String send = dis.readUTF();
System.out.println( "5555555555 ");
String subject = dis.readUTF();
String content = dis.readUTF();
try
{
Properties props = System.getProperties();
// Setup mail server
props.put( "mail.smtp.host ", host);
// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipient(Message.RecipientType.TO, new
InternetAddress(
send));
// Set the subject
message.setSubject(subject);
// Set the content
message.setText(content);
// Send message
Transport.send(message);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
[解决办法]
http://localhost:8888/cs/MaileServlet
如果你是说在IE上执行上面的URL的话那肯定报错,因为你是GET请求,没POST数据过去,会读不到数据,所以包IOEXCEPTION
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html