我試圖從java小應用程序訪問servlet並在小應用程序的文本字段中設置servlet的響應。 我使用的是tomcat 7.0,我的jre/jdk已經完全更新。 該servlet運行正常(在瀏覽器中正確的輸出)從瀏覽器爲localhost調用時:?8080 /打招呼/你好查詢= SELECT * FROM機場將小應用程序連接到servlet時格式錯誤的URL
(其中機場是數據庫的名稱)
然而,當我運行在appletviewer中的小程序,我得到拋出了錯誤的URL例外..
代碼小程序:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
/*
<applet code="lab101" width=500 height=270>
</applet>
*/
public class lab101 extends Applet implements ActionListener{
TextArea t;
Panel p,q,r;
CheckboxGroup c;
Checkbox ins,dis,del,update; //Checkboxes are included just for testing purposes.
TextField f;
Label l1;
Button b;
public void init(){
setLayout(new FlowLayout());
b=new Button("Run");
l1=new Label("Query:");
c=new CheckboxGroup();
t=new TextArea("",10,50);
p=new Panel();
q=new Panel();
r=new Panel();
p.add(t);
ins=new Checkbox("Insert",c,false);
dis=new Checkbox("Display",c,true);
del=new Checkbox("Delete",c,false);
update=new Checkbox("Update",c,false);
f=new TextField(50);
q.add(ins);
q.add(dis);
q.add(del);
q.add(update);
r.add(l1);
r.add(f);
r.add(b);
b.addActionListener(this);
add(p);
add(q);
add(r);
try{
URL url=new URL("127.0.0.1:8080/hello/hello?query=select * from airports");
URLConnection servletconnection=url.openConnection();
servletconnection.setDoInput(true);
InputStream in=servletconnection.getInputStream();
String s="";
int ch;
loop:while(1>0){
ch=in.read();
if(ch==-1) break loop;
else s+=(char)ch;
}
t.setText(s);
}//try close
catch(MalformedURLException e){
t.setText("Malformed URL Exception occured.");}
catch(IOException e){
t.setText("IO exception occured");}
}
public void actionPerformed(ActionEvent ae){
}
public void start(){
}
public void paint(Graphics g){
}
}//class ends
代碼的servlet:
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.sql.*;
public class hello extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/plain");
PrintWriter out=response.getWriter();
String query=request.getParameter("query");
Connection link=null;
Statement statement=null;
ResultSet results=null;
try{
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5432/test";
link=DriverManager.getConnection(url,"postgres","hooligan");
out.println("Successful connection");
}
catch(ClassNotFoundException e){
out.println("Unable to load driver");
}
catch(SQLException e){
out.println("Cannot connect to database");
}
try{
statement=link.createStatement();
//String select="select * from airports";
results=statement.executeQuery(query);
}
catch(SQLException e){
out.println("Cannot execute query");
e.printStackTrace();
}
try{
out.println();
while(results.next()){
out.println("Name: " + results.getString(1));
out.println("Location: " + results.getString(2));
//System.out.println("Account no: " + results.getInt(3));
System.out.println();}
}
catch(SQLException e){
out.println("Error retrieving data");
}
try{
link.close();}
catch(SQLException e){
out.println("Unable to disconnect");}
out.close();
out.flush();
}}
有什麼想法?
P.S.我也注意到,如果我使用127.0.0.1的本地主機,而不是我得到一個安全異常拋出
感謝您的回覆BalusC,但我仍然面臨着applet-servlet通信的問題。關鍵是applet的代碼庫是C:\ program files \ java \ jdk1.6.0_16 \ bin,servlet駐留在tomcat目錄的webapps中,c:\ tomcat \ webapps \ hello \ WEB-INF \ classes \ hello。類 我改變了網址,以包括URLEncoder的使用。我把這個小程序的html文件和編譯後的類文件放在hello目錄(c:\ tomcat \ webapps \ hello)中,URL爲 url = new URL(getCodeBase(),「hello?query =」+ query) ; 當我通過html文件運行它時,我在代碼中得到一個IO異常。 – user720694 2011-04-23 10:53:16
可能在applet中導致IOException的代碼部分可能是: 'code' InputStream in = servletconnection.getInputStream(); String s =「」; int ch; loop:while(1> 0){ ch = in.read(); (ch == - 1)break loop; else s + =(char)ch; } t.setText(s); 'code' 該servlet的響應返回爲out.println(queryresult);這需要通過Applet中的InputStream讀取。 當我評論涉及IO的代碼並運行小程序時,與servlet的連接成功。 – user720694 2011-04-23 10:56:01
您需要在網頁瀏覽器打開的網頁中運行小程序。該網頁必須由運行servlet的服務器提供服務。你不應該從命令行或applettester或別的東西單獨運行小程序。 – BalusC 2011-04-23 11:21:29