Velocity - Java Web Development New Technology

xiaoxiao2021-03-06  98

Velocity - Java Web Development New Technology

Keywords: Java, JSP, Servlet, Template, Template, Apache, Jakarta, Velocity Reader Requirements: Learn about Java Servlet Basic Concept

Velocity is a Java-based general template tool that comes from Jakarta.apache.org. The purpose of Apache development is to replace JSP / Struts.

JSP is a web application development technology developed by Sun, similar to PHP, and ASP. But there are many problems in JSP, making it difficult to promote, please refer to The Problems With JSP by Jason Hunter. Velocity is mainly used in the Java Servlet.

Template technology was first introduced as an additional toolkit in PHP. The aim is to solve the problems that are mixed with the common PHP code in PHP and HTML code, difficult to read, difficult to write, difficult to modify. In fact, this problem also exists in JSP / ASP. ASP is simple, introduced VB Script (the purpose of VBA's earliest design is to achieve each Office user, so super easy to learn, VB Script is similar to VBA syntax), making almost every one will learn to learn Java Script. . At the same time, there will be many people in ASP and HTML, but there are very few people with PHP and HTML, and there will be fewer people from JSP and HTML. Introducing Template in PHP, dividing HTML and PHP code into different files, will only change the php file in PHP, will only change the HTML file with HTML, and the division of labor is greatly enhanced, and the work efficiency is more easily written. Using Velocity, Java code is divided into different files with HTML code, do not learn JSP syntax. As one of the application technologies of MVC, Velocity is far more successful than JSP / Struts in Model and View code.

Please refer to my article to implement the HTML and PHP code separation using the IntegratedTemplate class in PHP4, and use the IntegratedTemplate class in PHP4 to implement the Block function.

Velocity can be used to generate dynamic web pages, SQL, PostScript files, and other text files that can be converted from templates. I used it to generate Email to send thank you letter. The difference between the Email sent to different users is the name of the username. It is easy to handle this thing with Velocity. First, use Dreamweaver to write an HTML format email.

Dear $ username,

....

The above variables are then replaced in the Java code and sent out. After the program is written, Email can also be modified with Dreamweaver without changing the Java code.

Velocity is used in the servlet, as follows:

1) Login.htm

???? User login

Welcome !! Login account: Password: > ???? 2 ) From VelocityServlet inherit, write a subclass login.java

???? mport javax.servlet.http. *;

Import org.apache.velocity. *;

Import org.apache.velocity.Context. *;

Import org.apache.velocity.servlet. *;

Public Class login

Extends velocityServlet {

protected template handlerequest

HTTPSERVLETREQUEST REQUEST,

HTTPSERVLETRESPONSE RESPONSE,

Context context) throws exception {

Boolean checksuccess = false;

// check

// ...

String Account = Request.getParameter ("TextFieldAccount");

String password = Request.getParameter ("textfieldpassword");

Checksuccess = Checkloginpassword (Account, Password);

Template Template = NULL;

Try {

Checksuccess {

Template = GetTemplate ("Success.htm");

Context.put ("UserName", getLoginuserName ()); // Replace $ username in HTML File

}

Else {

Template = GetTemplate ("fail.htm");

Context.put ("UserName", getLoginuserName ()); // Replace $ username in HTML File

}

}

Catch (Exception E) {

E.PrintStackTrace ();

}

Return Template;

}

Private Boolean CheckloginPassword (String Account, String Password) {

// Do Something to Check

// ....

Return True;

}

Private string getLoginuserName () {

Return "test by jack";

}

}

????

3) Success.htm

???? Untitled Document

CONGRATTIILATION !! You are logined as $ usrname !! ????

3) Fail.htm

???? Untitled Document

Error !! Fail to login as $ usrname !! ????

Basically, both success.htm and fail.htm can use Dreamweaver to use Dreamweaver, don't worry about Java code. Many of the JSP files cannot be used to use Dreamweaver typography, only HTML CODE, Velocity brings us into a new world. A Write a Web program using standard Java, with a standard HTML write representation, no tag, and there is no clear code world with custom syntax. And now there is no hateful TAG. JSP TAG makes many people feel inexplicably, and Java programmers look at the strange, web programmers look surprised. No a few people can master it, and there are not a few tools to check the TAG syntax error. If you write wrong, there is no tool to find the error is a disgusting thing.

Used Velocity, all of these troubles. Abandon the design poor JSP / Struts.

Author Blog:

http://blog.9cbs.net/jacklondon/

related articles

I am using Joshua Bloch Email Discussion (Emails Between Joshua Bloch and ME) C Singleton Realization Velocity Application Example Velocity - Java Web Develop New Technology Java JDBC Database Connection Pool Implementation Method

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

New Post(0)