JSP×Ô¶¨Òå±êÇ©±àд
Ò»¸ötag¾ÍÊÇÒ»¸öÆÕͨµÄjavaÀ࣬ËüΩһÌرðÖ®´¦ÊÇËü±ØÐë¼Ì³ÐTagSupport»òÕßBodyTagSupportÀà¡£ÕâÁ½¸öÀàÌṩÁËһЩ·½·¨£¬¸ºÔðjspÒ³ÃæºÍÄã±àдµÄÀàÖ®¼äµÄ½»»¥£¬ÀýÈçÊäÈ룬Êä³ö¡£¶øÕâÁ½¸öÀàÊÇÓÉjspÈÝÆ÷ÌṩµÄ£¬ÎÞÐ뿪·¢ÈËÔ±×Ô¼ºÊµÏÖ¡£»»¾ä»°Ëµ£¬ÄãÖ»Ðè°ÑʵÏÖÁËÒµÎñÂß¼µÄÀà¼Ì³ÐTagSupport»òÕßBodyTagSupport£¬ÔÙ×öһЩÌرðµÄ¹¤×÷£¬ÄãµÄÀà¾ÍÊÇÒ»¸öTag¡£²¢ÇÒËü×Ô¼º¸ºÔðºÍjsp Ò³ÃæµÄ½»»¥£¬²»ÓÃÄã¶à²ÙÐÄ¡£
“ÌرðµÄ¹¤×÷”ͨ³£ÓÐÒÔϼ¸¸ö²½Ö裺
[1]ÌṩÊôÐÔµÄset·½·¨£¬´ËºóÕâ¸öÊôÐԾͿÉÒÔÔÚjspÒ³ÃæÉèÖá£ÒÔjstl±êǩΪÀý c:out value=""/£¬Õâ¸övalue¾ÍÊÇjspÊý¾Ýµ½tagÖ®¼äµÄÈë¿Ú¡£ËùÒÔtagÀïÃæ±ØÐëÓÐÒ»¸ösetValue·½·¨£¬¾ßÌåµÄÊôÐÔ¿ÉÒÔ²»½Ðvalue¡£ÀýÈçsetValue(String data){this.data = data;}
Õâ¸ö“value”µÄÃû³ÆÊÇÔÚtldÀïÒÔattributeÔªËØ´æÔڵġ£
ȡʲôÃû×Ö¶¼¿ÉÒÔ£¬Ö»ÐètagÀïÌṩÏàÓ¦µÄset·½·¨¼´¿É¡£
[2]´¦Àí doStartTag »ò doEndTag ¡£ÕâÁ½¸ö·½·¨ÊÇ TagSupportÌṩµÄ¡£ »¹ÊÇÒÔc:out value=""/ΪÀý£¬µ±jsp½âÎöÕâ¸ö±êÇ©µÄʱºò£¬ÔÚ“<”´¦´¥·¢ doStartTag ʼþ£¬ÔÚ“>”ʱ´¥·¢ doEndTag ʼþ¡£Í¨³£ÔÚ doStartTag Àï½øÐгõʼ»¯,Á÷³ÌÑ¡Ôñ²Ù×÷£¬ÔÚ doEndTag ÀïºóÐøÒ³ÃæÊä³ö¿ØÖÆ¡£
[3]±àдtldÎļþ,¾ÍÊǰѱàдµÄtag×é¼þµÄÐÅÏ¢ÒÔmxlÐÎʽ¸æËßÈÝÆ÷,Ëü²ÅºÃÒÔÒ»¶¨²½Öè½âÊÍtag×é¼þ
[4]ÔÚjspÒ³Ãæµ¼Èëtld¡£ÕâÑù£¬ÄãµÄjspÒ³Ãæ¾Í¿ÉÒÔʹÓÃ×Ô¼ºµÄtag×é¼þÁË¡£
ͨ³£Äã»á·¢ÏÖ×Ô¼º¾ø´ó¶àÊý»î¶¯¶¼¼¯ÖÐÔÚ doStartTag »ò doEndTag·½·¨Àï,Èç¹ûÔÚ·þÎñÆ÷¶Ë´¦Àí±êÇ©ÖеÄÕýÎÄ»òÔòÊÇǶÌ×±êǩʱµÄ»°,»¹ÊǹýÎÊÒ»ÏÂdoAfterBody¡£
Ò»¸ö¼òµ¥µÄÀý×Ó£ºOutputTag
package test;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class OutputTag extends TagSupport {
private String name=null;
public void setName(String name) {
this.name = name;
}
public int doEndTag() throws JspException
{
try {
JspWriter out = pageContext.getOut();
out.print("Hello! " + name);
¡¡¡¡} catch (Exception e) { throw new JspException(e); }
return EVAL_PAGE;
}
}
Ïà¹ØÎĵµ£º
´ËʵÏÖ¹²·ÖÈý¸öÎļþ£¬¼´£ºCreatImage.java Éú³ÉͼÏñÁ÷µÄÀ࣬ÌṩÓÃÓÚÉú³ÉͼƬÁ÷µÄ·½·¨£»images.jsp ͼƬ°ü×°£¬ÓÃÓÚ½«Í¼Æ¬¼ÓÈëµ½responseÊä³öÁ÷ÖУ»login.jsp µ÷ÓÃÇ°ÃæµÄjsp£¬ÏÔʾÑéÖ¤Âë¡£
CreatImage.java Àࣺ
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.Graphics;
6 im ......
ÉÏһƪ·ÖÒ³ÎÄÕ£¬ÊÇÓÃÓÚmysql£¬ÉÔ΢ÐÞ¸ÄÏ£¬ÓÃÓÚSqlserver2005/2008£¬Ã»ÓÐÒì³£´¦Àí¡£Ã»Óп¼ÂÇÐÔÄܵȡ£
ÏÖ½«´úÂëÌù³ö£¬ÒÔ¹©³õѧÕ߲ο¼£º
×¢£ºÑûÔÂʹÓû·¾³Eclipse 3.4.2+Tomcat 6.18+Sqlserver2005 sp3µ÷ÊԳɹ¦¡£
Ò³ÃæpagelistDemo.jspÄÚÈÝ£º
Code
<%@ page language="java" contentType="text/html; ......
JspµÄ¾ÅÖÖ¶ÔÏóÎÒ¼òÊöÖ®£º
page
application
request
response
session
config
out
pageContext
page
<%@page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" %>
response
<%
response.setHeader("Pragma","No-cache");
respons ......
Action´úÂë
package com.zxc.action;import java.net.URLEncoder;public class XiaoZAction {
private String username; public String getUsername() { return username; }
public String execute() throws Exception{
username = URLEncoder.encode("ÖÜС´¨", "utf-8"); return "success"; }}
struts.x ......
JSPÖеÄÌøת£º
(1). forward()·½·¨
ʹÓõ½javax.servlet.RequestDispatcherÀà
RequestDispatcher rd = getServletContext().getRequestDispatcher("url");
rd.forward(requestVar,requestVar); //´«µÝservletµÄÈë¿Ú²ÎÊý
/*forwardº¯ÊýÒѾ°ÑÔÒ³ÃæµÄrequest,response¶ÔÏó´«ÈëеÄÒ³Ã棬Òò´ËÕâо ......