Velocity-New Java Web Development Technology

Source: Internet
Author: User
Tags dreamweaver

Velocity -- New Java Web Development Technology

 Keywords: Java, JSP, Servlet, template, template, Apache, Jakarta, Velocity
Reader requirements: Understand the basic concepts of Java Servlet

Velocity is a Java-based general template tool from jakarta.apache.org. Apache is designed to replace JSP/struts.

JSP is a Web application development technology developed by Sun. It has similar functions with PHP and ASP. However, JSP has many problems, making it difficult to promote. Please referThe problems with JSP by Jason hunter. Velocity is mainly used in Java Servlet.

The template technology was first introduced as an additional toolkit in PHP. The purpose is to solve the problem that common PHP code and HTML code in PHP are mixed together, which is hard to read, write, and modify. In fact, this problem also exists in JSP/asp. ASP introduces VB script because of its simple syntax. (The earliest Design of VBA is that every office user can learn it, so it is super easy to learn. VB script is similar to VBA syntax ), so that almost everyone who has a Java Script can learn it. At the same time, there are many ASP and HTML users, but few PHP and HTML users at the same time, and fewer JSP and HTML users at the same time. In PHP, templates are introduced to divide HTML and PHP code into different files. php only changes PHP files, HTML only changes HTML files, and the division of labor is clear. Therefore, the work efficiency is greatly improved, programs are easier to write. Using velocity, Java code and HTML code are divided into different files without learning JSP syntax. As one of MVC application technologies, velocity is far more successful than separating JSP/struts from view code in model.

For more information about PHP templates, see my article.Use the integratedtemplate class in PhP4 to separate HTML and PHP code,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 an email to send a thank-you letter. The difference between emails sent to different users is the starting user name. It is easy to use velocity to handle this matter. First, use Dreamweaver to write an HTML email.

Dear $ username,

....

Then, replace and send the above variables in the Java code. After the program is written, you can use Dreamweaver to modify the code without modifying the Java code.

An example of using velocity in servlet is as follows:

1) login.htm

? ? ?
?







form name="FormLogin" method="post" action="/login">

  Welcome !!  
       
  Login Account: Input name = "textfieldaccount" type = "text" id = "textfieldaccount">  
  Password: Input name = "textfieldpassword" type = "text" id = "textfieldpassword">  
    >  

?
? ? ?

2) inherit from velocityservlet and write a sub-class login. Java

? ? ?
?
import 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 {      if (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

? ? ?
?





Congratulation !!

You are logined as $ username !!

?
? ? ?

3) fail.htm

? ? ?
?





Error !!

Fail to login as $ username !!

?
? ? ?

Basically, both success.htm and fail.htm can be typeset with Dreamweaver without worrying about Java code. Compared to JSP files, Dreamweaver layout is not available in many places, and HTML code can only be modified manually. Velocity brings us into a new world. A clear code world that only uses standard Java to write web programs and uses standard HTML to write presentation layers without tags and custom syntax.

In addition, there are no annoying tag. jsp tags, which may be confusing to many people. Java programmers may look strange, and web programmers may also look strange. Few people are proficient in it, and few tools are able to check for tag syntax errors. If you write an error in that place, it is disgusting to have no tools to check for errors.

With velocity, all these troubles are gone. Give up the poorly designed JSP/struts.

Author's blog:Http://blog.csdn.net/jacklondon/Related Articles

My email discussion with Joshua BLOCH (emails between Joshua Bloch and me)
Singleton implementation in C ++
Application Example of Velocity
Velocity -- New Java Web Development Technology
Java JDBC database connection pool Implementation Method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.