JSP入门教程 下载本文

// 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中,要命名:

18. 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); } }

19. 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); }

}

20. 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); } }