JavaScript改变Applet状态
作者:东方一蛇/我的家
下面的例子将说明利用javascript去改变applet状态和在javascript中调用applet中的方法。 一、applet源代码(tmin_JS2.java) // Import
import java.awt.Graphics ; import java.awt.Event ;
// LiveConnect... for JavaScript // Not used... (it's more simple)
public class tmin_JS2 extends java.applet.Applet { // Variables
String str ; // Sample string... int i ; // nb change...
// Initialisation de l'applet
public void init() { // Methode init() str = new String(\i = 0 ; }
// Dessiner l'applet
public void paint(Graphics g) { // Methode paint() g.drawString(str, 5, 10) ; }
// setString : change string value public void setString(String s) { str = new String( s ); i++ ;
// force repaint to see change repaint() ; return ; }
// getString : get string value public String getString() { return str ; }
// getVal : get number of change public int getVal() { return i ;
} }
二、注意的地方
(1)在applet中,要命名:
JSP源代码实例一
转:jsp源码实例(搜索引擎) Marty Hall [2001-01-06]
作者:Marty Hall package coreservlets; import java.io.*;
import javax.servlet.*; import javax.servlet.http.*; import java.net.*;
public class SearchEngines extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String searchString = request.getParameter(\ if ((searchString == null) || (searchString.length() == 0)) {
reportProblem(response, \ return; }
// The URLEncoder changes spaces to \ // non-alphanumeric characters to \ // hex value of the ASCII (or ISO Latin-1) character. // Browsers always URL-encode form values, so the // getParameter method decodes automatically. But since // we're just passing this on to another server, we need to // re-encode it.
searchString = URLEncoder.encode(searchString);
String numResults = request.getParameter(\ if ((numResults == null) || (numResults.equals(\ (numResults.length() == 0)) {
numResults = \ }
String searchEngine =
request.getParameter(\ if (searchEngine == null) {
reportProblem(response, \ return; }
SearchSpec[] commonSpecs = SearchSpec.getCommonSpecs(); for(int i=0; i if (searchSpec.getName().equals(searchEngine)) { String url = searchSpec.makeURL(searchString, numResults); response.sendRedirect(url); return; } } reportProblem(response, \ } private void reportProblem(HttpServletResponse response, String message) throws IOException { response.sendError(response.SC_NOT_FOUND, \ } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } JSP源代码实例二 转:jsp源码实例(获取jsp各种参数) Marty Hall [2001-01-06] 作者:Marty Hall package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; /** Creates a table showing the current value of each * of the standard CGI variables. * * Taken from Core Servlets and JavaServer Pages * from Prentice Hall and Sun Microsystems Press, * http://www.coreservlets.com/. * © 2000 Marty Hall; may be freely used or adapted. */ public class ShowCGIVariables extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ PrintWriter out = response.getWriter(); String[][] variables = { { \ { \ String.valueOf(request.getContentLength()) }, { \ { \ getServletContext().getRealPath(\ { \ { \ { \ { \ { \ { \ { \ { \ { \ { \ String.valueOf(request.getServerPort()) }, { \ { \ getServletContext().getServerInfo() } }; String title = \ out.println(ServletUtilities.headWithTitle(title) + \ \ \ \ \ for(int i=0; i varValue = \ out.println(\ } out.println(\ } /** POST and GET requests handled identically. */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } JSP源代码实例三 转:jsp源码实例(获取表单参数) Marty Hall [2001-01-06] 作者:Marty Hall package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; /** Shows all the parameters sent to the servlet via either * GET or POST. Specially marks parameters that have * no values or multiple values. * * Taken from Core Servlets and JavaServer Pages * from Prentice Hall and Sun Microsystems Press, * http://www.coreservlets.com/. * © 2000 Marty Hall; may be freely used or adapted. */ public class ShowParameters extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ PrintWriter out = response.getWriter(); String title = \ out.println(ServletUtilities.headWithTitle(title) + \ \ \ \ \ Enumeration paramNames = request.getParameterNames(); while(paramNames.hasMoreElements()) { String paramName = (String)paramNames.nextElement(); out.print(\ String[] paramValues = request.getParameterValues(paramName); if (paramValues.length == 1) { String paramValue = paramValues[0]; if (paramValue.length() == 0) out.println(\ else out.println(paramValue); } else { out.println(\ for(int i=0; i out.println(\ } } out.println(\ } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }