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.