Write "Pure HTML" JSP Apps - learn to use JSTL

xiaoxiao2021-03-06  63

When you start using JSP to do web applications, you will use the PHP, ASP. Use JSP as you, mix a lot of Scriptlet in the HTML code --- Java code segment. Since I can "typeset" technology, I feel very good, write "efficiency" high, debugging and convenient! It later, I only knew that - this is an extremely stupid approach. When JSP is born, it has been clearly clarified with the ASP, PHP, using JavaBean, servlet can effectively put the java code in HTML. Segment peeling, then packaged into a "logical processing component" that can be reused in multiple pages - this is one of the advantages of JSP relative to PHP and ASP. But sometimes even if we use JavaBean servlet, we have to embed the "very small amount" Java code in HTML -, sometimes you have this: Because you can't use the implied object in JSP, For example, REQUEST, SESSION, RESPONS, etc. Work with Servlet, although JSP objects can be used, but can not be flexibly inserted into HTML in JavaBean. Its appearance completely solves this problem, allowing you to write "pure html" JSP page number - the benefits brought about by this self-evident: higher maintenanceability, higher component multiplexing efficiency , More easy to maintain the HTML page `` `` The younger brother is not talented, just start learning JSTL, I think this Dongdong is really good! I want to let more beginners know this, and can be applied to the actual web development. Below, we start writing our first TAG! ** The following is the JSP file using simple TAG, the result is the current time: <% @ Page ContentType = "text / html; charset = GB2312"%> <% @ Taglib URI = "/ Tags "prefix =" VISA "%> The current time is: It is clear that the JSP page for TAG is refreshed." If database operation, some complex features such as database operations If you also package it, the advantage of tag is more obvious! ** Environment: Win2000 Server Tomcat 5.019 J2SDK1.42 SQLServer 2k ** Develop a TAG, you need to write 2 main files: 1-label processor (a class servlet's Java class) 2-label descriptor (one XML style TLD files) Finish these two files, you can deploy, apply it in web applications. Ok, let's start doing it! 1 - Write a TAG processor: DateTag.java It works like a servlet, accepts requests from the client, but it can be called in JSP as JavaBean.

package tag; import java.util.Date; import javax.servlet.jsp *;. import javax.servlet.jsp.tagext *;. public class datetag extends TagSupport {public int doStartTag () throws JspException {Date dte = new Date ( ); TryContext.get (); out.print (dte);} catch (java.ioException e) {throw new jsptagexception (E.getMessage ());} return skip_body;}} with javac After compiling, you get the datetag.class file, put it in the XXX / Web-INF / CLASSES / TAG directory. 2 - Write a Tag library descriptor: tags.TLD is readily see that section is a bit like servlet mapping configuration - the mapping between TAG's name and TAG class is configured. 1.0 1.2 < Name> Date tag.datetag Empty saving tags.tld in xxx / web -Inf / directory.

转载请注明原文地址:https://www.9cbs.com/read-118940.html

New Post(0)