用Tomcat搭建GSP服务器,IDEA开发GSP

 

新建一个Web项目

(一)将antlr、asm、groovy三个jar包放到WEB-INF\lib下,项目目录结构如下

(二)修改web.xml,增加serverlet和serverlet映射为以下内容,它的意思就是告诉服务器,凡是*.groovy文件,用groovy.servlet.GroovyServlet类处理,凡是*.gsp用groovy.servlet.TemplateServlet类处理

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <servlet>
        <servlet-name>GroovyServlet</servlet-name>
        <servlet-class>groovy.servlet.GroovyServlet</servlet-class>
    </servlet>

    <servlet>
        <servlet-name>GroovyTemplate</servlet-name>
        <servlet-class>groovy.servlet.TemplateServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>GroovyServlet</servlet-name>
        <url-pattern>*.groovy</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>GroovyTemplate</servlet-name>
        <url-pattern>*.gsp</url-pattern>
    </servlet-mapping>

</web-app>

 

 (三)为了方便建立gsp文件,增加IDEA的groovy文件和GSP文件模板,注意,gsp要去掉文件开头的注释,不要有以下注释,因为可能会在报错,不能识别。

 

<%--
Created by IntelliJ IDEA.
User: ${USER}
Date: ${DATE}
Time: ${TIME}
To change this template use File | Settings | File Templates.
--%>

 

 完整的gsp页面如下,尽量不要注释,这可能会成为错误的一个原因:

 

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head><title>Simple GSP page</title></head>
<body>Place your content here</body>
</html>

 

 (四)修改tomcat的配置文件,web.xml,以增加对index.gsp的支持

 

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file> 
     <welcome-file>index.gsp</welcome-file>
</welcome-file-list>

 (五)在根目录WEB下创建index.gsp,好了启动Tomcat服务,http://localhost:8080/是不是已经可以显示你的index.gsp了?

(六)创建一个hello.groovy,如在Web\groovy\目录下创建一个hello.groovy,简单的输出打印就可以

 

println '''  
<html><head> 
<title>Hello Groovy</title> 
</head> 
<body> 
<p> 
Welcome to Groovlets 101. As you can see 
this Groovlet is fairly simple. 
</p> 
<p> 
This course is being run on the following servlet container: </br> 
$ {application.getServerInfo ()}  
</p> 
</body> 
</html> 
'''

 然后在地址栏输入http://localhost:8080/groovy/hello.groovy,是不是已经测试成功了?

 

一个简单的GSP服务器,就此搭建成功!不管是使用哪种IDE,都可以完成以上操作,个人只是比较喜好IDEA而已~

 

 

你可能感兴趣的:(tomcat,servlet,javaee,groovy,idea)