| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1069 人关注过本帖
标题:求助:急! 关于“使用了未经检查或不安全的操作”
只看楼主 加入收藏
OAK
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-3-14
收藏
得分:0 

还在吗


不好意思

我刚才网络不好 一直上不去

代码:
import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.net.*;
import java.io.*;
import java.util.Vector;
import java.net.MalformedURLException;

public class chatApplet extends Applet implements Runnable,ActionListener{
Label userInfo;
Label messageInfo;
Label listInfo;
Button sendButton;
TextField userText;
TextArea messageText;
List userList;
URL chatURL;
URLConnection connect;
String username;
Thread pollThread=null;
volatile private boolean loggedin=false;
String servletPath;

public synchronized void inti(){
int newPriority;
if(pollThread!=null)
return;
super.init();
resize(500,300);
userInfo=new Label("Enter User Name (max 10 chars):");
messageInfo=new Label("message text:");
userText=new TextField(40);
sendButton=new Button("send");
messageText=new TextArea(10,40);
messageText.setEditable(false);
userText.addActionListener(this);
sendButton.addActionListener(this);
Panel mainp=new Panel();
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
gbc.weightx =0;
gbc.weighty =0;
gbc.gridx=0;
gbc.gridy=0;
gbc.gridheight =1;
gbc.gridwidth=10;
gbc.anchor =GridBagConstraints.CENTER;
gbc.fill =GridBagConstraints.NONE ;
mainp.setLayout(gbl);
gbl.setConstraints(userInfo, gbc);
mainp.add(userInfo);

gbc.gridy=1;
gbc.gridwidth=9;
gbc.fill =GridBagConstraints.HORIZONTAL ;
gbl.setConstraints(userText, gbc);
mainp.add(userText);

gbc.gridx=9;
gbc.gridwidth=1;
gbc.fill =GridBagConstraints.NONE ;
gbl.setConstraints(sendButton, gbc);
mainp.add(sendButton);

gbc.gridx=0;
gbc.gridy=2;
gbc.gridwidth=10;
gbl.setConstraints(messageInfo, gbc);
mainp.add(messageInfo);

gbc.gridy=3;
gbc.weighty=100;
gbc.gridheight=10;
gbc.fill =GridBagConstraints.BOTH ;
gbl.setConstraints(messageText, gbc);
mainp.add(messageText);

Panel userp=new Panel();
userp.setLayout(new BorderLayout());
listInfo=new Label("users logged in:");
userp.add(listInfo, "North");
userList=new List(10,false);
userList.add("nont logged in");
userp.add(userList, "Center");
setLayout(new BorderLayout());
add(mainp,"Center");
add(userp,"East");
servletPath=getParameter("serverPath");
try{
chatURL=new URL(servletPath);

}catch (MalformedURLException e){

}
int currPriority=Thread.currentThread().getPriority();
if(currPriority==Thread.MIN_PRIORITY)
newPriority=Thread.MIN_PRIORITY;
else
newPriority=currPriority-1;
pollThread=new Thread(this,"ChatPoll");
pollThread.setDaemon(true);
pollThread.setPriority(newPriority);
pollThread.start();
p("chat:getting the information from chatServlet.java");

}
public synchronized void start(){
if(!isLoggedin()&&username!=null){
login();
}
if(pollThread==null||!pollThread.isAlive()){
p("chat :no poll thread!");
pollThread=new Thread(this,"Chatpoll");
pollThread.setDaemon(true);
pollThread.start();
p("chat: starting poll");

}
}
public synchronized void stop(){
if(pollThread.isAlive()){
try {
pollThread.wait();
p("chat: suspending poll");
}catch(InterruptedException e){

}
}else{
p("chat: pollThread die");
}
logout();
}
public synchronized void destroy(){
if(pollThread!=null&&pollThread.isAlive()){
pollThread=null;
p("chat:stoppint poll");
}
logout();
}
public void run(){
p("chat:starting the poll run");
while(!Thread.interrupted()){
if(isLoggedin()){
pollList();
poll();
p("chat:poll");
}else{
pollList();
p("chat:not loggedin for poll");

}
try{
Thread.sleep(1000*3);
}catch(InterruptedException e){

}
}
p("chat:exting run()");
}
private void login(){
String queryString="";
if(username==null)
return;
try{
queryString="chatServlet?mode=login&user="+URLEncoder.encode(username, "UTF-8");

}catch(UnsupportedEncodingException e){

}
p("Attempting login as "+username);
try{
connect=(new URL(chatURL,queryString)).openConnection();
connect.setDefaultUseCaches(false);
connect.setUseCaches(false);
connect.setDoInput(true);
connect.setDoOutput(false);
connect.connect();
p("made connection to"+connect);
BufferedReader in=new BufferedReader(
new InputStreamReader(connect.getInputStream()));
String response=in.readLine();
if(response.startsWith("+")){
setLoggedin(true);
showStatus("Logged into chat as user:"+username);
userInfo.setText("type message:");
p("logged in as "+username);
repaint();
}else {
showStatus("error logging in"+response);
p("could not log in as "+username);
System.err.println("error logging in"+response);

}
}catch (MalformedURLException e1){
System.err.println("MalformedURLException logging in!");
e1.printStackTrace(System.err);
showStatus("error logging in");
}catch(IOException e2){
System.err.println("IOException logging in!");
e2.printStackTrace(System.err);
showStatus("error logging in");
}
}
private void logout(){
String queryString="";
if(username==null||!isLoggedin())
return;
try{
queryString="chatServlet?mode=login&user="+URLEncoder.encode(username, "UTF-8");

}catch(UnsupportedEncodingException e){

}
p("Attempting login as "+username);
try{
connect=(new URL(chatURL,queryString)).openConnection();
connect.setDefaultUseCaches(false);
connect.setUseCaches(false);
connect.setDoInput(true);
connect.setDoOutput(false);
connect.connect();

BufferedReader in=new BufferedReader(
new InputStreamReader(connect.getInputStream()));
String response=in.readLine();
if(response.startsWith("+")){
setLoggedin(false);
showStatus("user:"+username+"logged out of chat");

}else {
showStatus("error logging out "+response);

System.err.println("error logging out"+response);

}
}catch (MalformedURLException e1){
System.err.println("MalformedURLException logging out!");
e1.printStackTrace(System.err);
showStatus("error logging in");
}catch(IOException e2){
System.err.println("IOException logging out!");
e2.printStackTrace(System.err);
showStatus("error logging in");
}

}
private void send(){
String queryString="";
String message=userText.getText();
if(message.equals(""))
return;
userText.setText("");
showStatus("sending message");

try{
queryString="chatServlet?mode=send&user="+URLEncoder.encode(username, "UTF-8");
queryString=queryString+"&message="+URLEncoder.encode(username, "UTF-8");
}catch(UnsupportedEncodingException e){

}
p("Attempting login as "+username);
try{
connect=(new URL(chatURL,queryString)).openConnection();
connect.setDefaultUseCaches(false);
connect.setUseCaches(false);
connect.setDoInput(true);
connect.setDoOutput(false);
connect.connect();

BufferedReader in=new BufferedReader(
new InputStreamReader(connect.getInputStream()));
String response=in.readLine();
if(response.startsWith("+")){

showStatus("message is send");

}else {
showStatus("error sending message "+response);

System.err.println("error logging out"+response);

}
}catch (MalformedURLException e1){
System.err.println("MalformedURLException logging out!");
e1.printStackTrace(System.err);
showStatus("error logging in");
}catch(IOException e2){
System.err.println("IOException logging out!");
e2.printStackTrace(System.err);
showStatus("error logging in");
}


}
private void poll(){
String queryString="";
if(username==null)
return;
try{
queryString="chatServlet?mode=poll&user="+URLEncoder.encode(username, "UTF-8");

}catch(UnsupportedEncodingException e){

}
try{
URL url=new URL(chatURL,queryString);
BufferedReader in=new BufferedReader(
new InputStreamReader(url.openStream()));
String nextLine=in.readLine();
if(!nextLine.startsWith("+")){
setLoggedin(true);
showStatus("error getting message from server");
System.err.println("error getting message from server");

}
nextLine=in.readLine();
while(nextLine!=null&&!nextLine.equals(".")){
System.err.println(nextLine);
messageText.append(nextLine+"\r\n");
repaint();
nextLine=in.readLine();
}
}catch(IOException e){
System.err.println("IOException !");
e.printStackTrace(System.err);
showStatus("error ");

}
}
private void pollList(){
String queryString="chatServlet?mode=list";
Vector<Object> users=new Vector<Object>();
try{
URL listURL=new URL(chatURL,queryString);
URLConnection listConn=listURL.openConnection();
listConn.setDefaultUseCaches(false);
listConn.setUseCaches(false);
listConn.connect();
BufferedReader in=new BufferedReader(
new InputStreamReader(listConn.getInputStream()));
String nextLine=in.readLine();
if(!nextLine.startsWith("+")){

showStatus("error getting userlist from server");
p("error getting userlist from server");
return;

}
nextLine=in.readLine();
while(nextLine!=null&&!nextLine.equals(".")){
p("read user:"+nextLine);
users.addElement(nextLine);
nextLine=in.readLine();
}
if(!users.isEmpty()){
userList.removeAll();
int size=users.size();
for (int i=0;i<size;i++){
userList.add((String)users.elementAt(i));
}
}else{
userList.removeAll();
userList.add("none logged in");
}
repaint();
}catch(IOException e){
System.err.println("IOException !");
e.printStackTrace(System.err);
showStatus("error logging in");
}

}

public boolean isLoggedin(){
return loggedin;
}
protected void setLoggedin(boolean newval){
loggedin=newval;
}
public void actionPerformed(ActionEvent evt){
if(evt.getSource()==sendButton||evt.getSource()==userText){
if(!isLoggedin())
send();
else{
username=userText.getText();
if(username.length()>10){
showStatus("10 or fewer characters,please");
}else{
userText.setText("");
login();
}
}
}
}
private void p(String debug){
System.err.println("chat:"+debug);
}


}

2007-05-19 11:51
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
chat:chat :no poll thread!
chat:chat: starting poll
chat:chat:starting the poll run
IOException !
java.net.MalformedURLException: no protocol: chatServlet?mode=list
at java.net.URL.<init>(URL.java:567)
at java.net.URL.<init>(URL.java:464)
at chatApplet.pollList(chatApplet.java:337)
at chatApplet.run(chatApplet.java:151)
at java.lang.Thread.run(Thread.java:619)
chat:chat:not loggedin for poll
java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:485)
at chatApplet.stop(chatApplet.java:126)
at sun.applet.AppletPanel.run(AppletPanel.java:518)
at java.lang.Thread.run(Thread.java:619)


这是我电脑上抛出的异常

代码有问题

可惜不是你,陪我到最后
2007-05-19 11:53
OAK
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-3-14
收藏
得分:0 
Applet程序和Servlet程序编译环境 不一样嘛

我全是在dos的环境下编译的

这可以吗
2007-05-19 11:54
OAK
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-3-14
收藏
得分:0 

我运行时也抛出了一样的异常

但看了代码 后,还是找不出 那错了

但是最起码 那些控件应该显示吧

2007-05-19 11:57
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
抛 了异常,程序就不会正常的顺序执行了

可惜不是你,陪我到最后
2007-05-19 12:01
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
你把init写成inti了

当然这个inti方法就不会被调用了


可惜不是你,陪我到最后
2007-05-19 12:06
syuanq
Rank: 2
等 级:新手上路
威 望:3
帖 子:297
专家分:0
注 册:2006-12-11
收藏
得分:0 
public synchronized void inti() 这里inti要改为init

[url]www.[/url]欢迎大家的光临,一起交流学习
2007-05-19 12:28
快速回复:求助:急! 关于“使用了未经检查或不安全的操作”
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016471 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved