当前位置: 技术问答>java相关
applet与servlet实时数据交互的问题
来源: 互联网 发布时间:2015-07-06
本文导语: 需要applet将少量参数传递给servlet,servlet根据参数提取数据库中地数据,并进行处理,处理后转还给applet显示,, 怎样弄,,望有原码 另外,我需要调试servlet,但修改servlet后,总要重起服务器,才能调试出来,,...
需要applet将少量参数传递给servlet,servlet根据参数提取数据库中地数据,并进行处理,处理后转还给applet显示,,
怎样弄,,望有原码
另外,我需要调试servlet,但修改servlet后,总要重起服务器,才能调试出来,,不然还是出现未修改之前地结果。
怎样弄,,谢谢
看看,,98分阿,,多吉利,,快快,,等待
怎样弄,,望有原码
另外,我需要调试servlet,但修改servlet后,总要重起服务器,才能调试出来,,不然还是出现未修改之前地结果。
怎样弄,,谢谢
看看,,98分阿,,多吉利,,快快,,等待
|
http://www.cn-java.com/target/news.php?news_id=1328
|
1.
//Import Servlet Libraries
import javax.servlet.*;
import javax.servlet.http.*;
//Import Java Libraries
import java.util.*;
import java.sql.*;
import java.io.*;
public class DbServlet extends HttpServlet {
Connection dbCon;
public void init() throws ServletException {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dbURL = "jdbc:odbc:projects";
dbCon = DriverManager.getConnection(dbURL);
} catch (Exception e) {
System.out.println("Database connect failed (init)");
System.out.println(e.toString());
return;
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
res.setContentType("text/html");
String qry = req.getParameter("qry");
try {
Statement s = dbCon.createStatement();
ResultSet rs = s.executeQuery(qry);
while (rs.next()) {
out.println(rs.getString(1) + " - " + rs.getString(2));
out.println(rs.getString(3));
out.println ("");
}
} catch (SQLException e) {
System.out.println(e.toString());
return;
}
out.println();
out.close();
}
public void destroy() {
/* Close database connection */
try {
dbCon.close();
} catch (Exception e) {
System.out.println("Error closing database (destroy)");
System.out.println(e.toString());
}
}
}
////////////////////////////////////////////////////////
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;
public void init() {
Panel p1 = new Panel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(new Label("Query String:"));
tfQuery = new TextField("", 50);
p1.add(tfQuery);
btnExecute = new Button("Execute Query");
btnExecute.addActionListener(this);
p1.add(btnExecute);
add("North", p1);
taResults = new TextArea(10, 80);
add("Center", taResults);
}
public void executeQuery() {
String qryString = tfQuery.getText();
try {
/* The line below can be adjusted to your local servlet position */
URL url = new URL("http://localhost:8080/servlet/DbServlet");
String qry = URLEncoder.encode("qry") + "=" +
URLEncoder.encode(qryString);
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
uc.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
DataOutputStream dos = new DataOutputStream(uc.getOutputStream());
dos.writeBytes(qry);
dos.flush();
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());
}
}
public void actionPerformed(ActionEvent ae) {
executeQuery();
}
}
2.将SERVER的工作目录下的东东都删除!
//Import Servlet Libraries
import javax.servlet.*;
import javax.servlet.http.*;
//Import Java Libraries
import java.util.*;
import java.sql.*;
import java.io.*;
public class DbServlet extends HttpServlet {
Connection dbCon;
public void init() throws ServletException {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dbURL = "jdbc:odbc:projects";
dbCon = DriverManager.getConnection(dbURL);
} catch (Exception e) {
System.out.println("Database connect failed (init)");
System.out.println(e.toString());
return;
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
res.setContentType("text/html");
String qry = req.getParameter("qry");
try {
Statement s = dbCon.createStatement();
ResultSet rs = s.executeQuery(qry);
while (rs.next()) {
out.println(rs.getString(1) + " - " + rs.getString(2));
out.println(rs.getString(3));
out.println ("");
}
} catch (SQLException e) {
System.out.println(e.toString());
return;
}
out.println();
out.close();
}
public void destroy() {
/* Close database connection */
try {
dbCon.close();
} catch (Exception e) {
System.out.println("Error closing database (destroy)");
System.out.println(e.toString());
}
}
}
////////////////////////////////////////////////////////
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;
public void init() {
Panel p1 = new Panel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(new Label("Query String:"));
tfQuery = new TextField("", 50);
p1.add(tfQuery);
btnExecute = new Button("Execute Query");
btnExecute.addActionListener(this);
p1.add(btnExecute);
add("North", p1);
taResults = new TextArea(10, 80);
add("Center", taResults);
}
public void executeQuery() {
String qryString = tfQuery.getText();
try {
/* The line below can be adjusted to your local servlet position */
URL url = new URL("http://localhost:8080/servlet/DbServlet");
String qry = URLEncoder.encode("qry") + "=" +
URLEncoder.encode(qryString);
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
uc.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
DataOutputStream dos = new DataOutputStream(uc.getOutputStream());
dos.writeBytes(qry);
dos.flush();
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());
}
}
public void actionPerformed(ActionEvent ae) {
executeQuery();
}
}
2.将SERVER的工作目录下的东东都删除!