回复: Java Applet can not communicate with Java Servlet

2013-04-30 Thread qingtao peng
Have you any document for me to consult?



 发件人: Christopher Schultz ch...@christopherschultz.net
收件人: Tomcat Users List users@tomcat.apache.org 
发送日期: 2013年4月29日, 星期一, 11:01 下午
主题: Re: Java Applet can not communicate with Java Servlet
 

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256

Peng,

On 4/29/13 10:05 AM, qingtao peng wrote:
 uc.setRequestProperty(Content-type,application/xwww-form-urlencoded);

I
 
think you want:

application/x-www-form-urlencoded

 res.setContentType(text/html;charset=GB2312);

Why not use UTF-8 like you did in the Applet? What you have done is
not wrong and it should work, but it does seem a little odd to use
different character encodings in different places.

 I have started the apache-tomcat-7.0.32 server ,then I have typed
 the network address of DbApplet.htm in the  address field of the 
 browser.I click on the button in the Applet,but  receive 
 java.io.FileNotFoundException error.The error is 
 java.io.FileNotFoundException: 
 http://localhost:8080/Servlet/DbServlet.Where do I place DbServlet
 ?

The servlet spec says that all servlets need to be in a package. So
you should have a package statement at the top of your .java file,
then re-compile.

 That is, which directory  do I place under in
 apache-tomcat-7.0.32.

Assuming your package is my.pkg, then you want your .class file here:

.../mywebapp/WEB-INF/classes/my/pkg/DbServlet.class

 What is the network address of this Servlet .I thank for helps.

Network address (hostname) will be wherever you configured Tomcat to
listen (localhost? www.foo.com?). The URL will depend upon how you
have mapped the servlet in web.xml. Have you done that?

- -chris
-BEGIN PGP SIGNATURE-
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJRfotFAAoJEBzwKT+lPKRYOLMP/0XuXo5m06m6cXUfv90Ngh+U
Vpy4L9y8WCWpHthtJqxk4EDRLT1K0Y5hV6cW0S7BzeH9ynh2VaKVvcWMR3KPcwts
Pc60/TUMj7qGLzXkTfBxxUnWGzY6fQLq4KRJdiHDQvycnw/KwYbM1FjDU9PjG2vi
yeEZQhEqMrP+fHCq8bYLAH+nuqngIGt59HGlN+1Umz5rsguSu6uq/ymaXUYEzyXZ
k1lqDwKSTGn7x4LD+AOqzJxH549bMjFVqexE5Fw4yFCfxChAP7zQwnDhL1jkKcsB
TXZJKQ5wNqQmHYg/C/9Cf+jKg7wmF6nEyw3FB4zDcYhKnGXaBBq2S8zF61Y8wKq3
QPkbyMfC8zPivXC1Jx28TyDJVQ81SLyKGB9JYfLZSuGvpN3rEV5sXOVrwKZw4+D7
mRYrwCYshm2/ueFALPXEjZbbAQHiXSqISDSdiMxYbIazMOCgxwcWcBfh7Rd2/g5+
rWU/xDdM0rUaIEkRVfA5bVQg9zNOqqoZFe7aEFNlBK0Y02LY2G9Bi2VtrfVMVnHo
gXxf0efIn7fyI9ajsh1JFm/f+NxKvZissEPqDRr7pWV0qyqcr8YfErrPT6jYMPl9
lGSF2RIFXO2zaxJ2x6pwnLxWl/EYXxayFPqoCQXTw3lBRLaCeVG+Ftw+l8ni+Gm7
aIzoBlfT46MVINdwePvB
=UAT7
-END PGP SIGNATURE-

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Java Applet can not communicate with Java Servlet

2013-04-29 Thread qingtao peng
  I have built the apache-tomcat-7.0.32 server on my computer.My operating 
system is Windows7.I have written  an Applet and compiled it .The  source code 
of the Applet is as follow :

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class DbApplet extends Applet implements ActionListener
{
TextField tfQuery;
TextArea taResults;
Button btnExecute;
URL chatURL;
public void init()
{
Panel pa = new Panel();
pa.setLayout(new FlowLayout(FlowLayout.LEFT));
pa.add(new Label(查询串:));
tfQuery = new TextField(SELECT number,code,score from chengji WHERE 
code='3001',50);
pa.add(tfQuery);
btnExecute = new Button(查询);
btnExecute.addActionListener(this);
pa.add(btnExecute);
add(North,pa);
taResults = new TextArea(30,60);
add(Center,taResults);
chatURL = getCodeBase();
}
public void actionPerformed(ActionEvent evt)
{
String lbl = evt.getActionCommand();
if(lbl.equals(查询))
{
String qryString = tfQuery.getText();
try
{
String qry = URLEncoder.encode(qry,UTF-8) + = +
URLEncoder.encode(qryString,UTF-8);
String str = http://localhost:8080/Servlet/DbServlet;;
URL urlName = new URL(str);
URLConnection uc = urlName.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
uc.setRequestProperty(Content-type,application/xwww-form-urlencoded);
DataOutputStream dos = new
DataOutputStream(uc.getOutputStream());
dos.writeBytes(qry);
dos.close();
InputStreamReader in = new
InputStreamReader(uc.getInputStream());
int chr = in.read();
while(chr != -1)
{
taResults.append(String.valueOf((char)chr));
chr = in.read();
}
in.close();
}
catch(MalformedURLException e)
{
taResults.setText(e.toString());
}
catch(IOException e)
{
taResults.setText(e.toString());
}
}
}
}
This compiled Applet has been embedded in a HTML file named with DbApplet.htm.
The  source code of the Servlet is as follow : 


import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DbServlet
 */
public class DbServlet extends HttpServlet {
/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 
response)
 */
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws 
ServletException, IOException {
PrintWriter out = res.getWriter();
res.setContentType(text/html;charset=GB2312);
String qry = req.getParameter(qry);
qry = URLDecoder.decode(qry,UTF-8);
out.println(qry);
Connection dbCon = null;
try
{
Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);
String dbURL = jdbc:odbc:STU;
dbCon = DriverManager.getConnection(dbURL,,);
PreparedStatement p = dbCon.prepareStatement(qry);
ResultSet rs = p.executeQuery();
while(rs.next())
{
out.print(rs.getString(1));
out.print(rs.getString(2) +  );
out.println(rs.getInt(3));
}
}
catch(Exception e)
{
out.println(读写数据库出错: + e.getMessage());
}
finally
{
try
{
dbCon.close();
out.close();
}
catch(Exception e)
{
out.println(关闭数据库出错: + e.getMessage());
}
}
// TODO Auto-generated method stub
}

}

I have started the apache-tomcat-7.0.32 server ,then I have typed the network 
address of DbApplet.htm in the  address field of the browser.I click on the 
button in the Applet,but  receive java.io.FileNotFoundException error.The error 
is java.io.FileNotFoundException: http://localhost:8080/Servlet/DbServlet.Where 
do I place DbServlet ? That is, which directory  do I place under in 
apache-tomcat-7.0.32. What is the network address of this Servlet .I thank for 
helps.