标签:
<html>
<head>
<title>test</title>
</head>
<body>
Hello world
</body>
</html>
helloworld.jsp<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>jsp test</title>
</head>
<body bgcolor="#fdf5e6">
<h1>Hello JSP.</h1>
Time:<%= new java.util.Date() %>
</body>
</html>
浏览器访问:import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Simple servlet used to test server.*/
public class HelloServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String docType="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "+"Transitional//EN\">\n";
out.println(docType+
"<html>\n"+
"<head><title>Hello</title></head>\n"+
"<body bgcolor=\"#fdf5e6\">\n"+
"<h1>Hello world</h1>\n"+
"</body></html>");
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
this.doGet(request,response);
}
}
编译HelloServlet.java<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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_3_0.xsd"
version="3.0"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>
启动Tomcat,在浏览器访问,如下,则说明正确package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Simple servlet used to test server.*/
public class HelloServlet2 extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String docType="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "+"Transitional//EN\">\n";
out.println(docType+
"<html>\n"+
"<head><title>Hello</title></head>\n"+
"<body bgcolor=\"#fdf5e6\">\n"+
"<h1>Hello world (2)</h1>\n"+
"</body></html>");
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
this.doGet(request,response);
}
}
<servlet>
<servlet-name>hello2</servlet-name>
<servlet-class>coreservlets.HelloServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello2</servlet-name>
<url-pattern>/HelloServlet2</url-pattern>
</servlet-mapping>
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Simple servlet for testing the use of packages
*and utilities from the same package*/
public class HelloServlet3 extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String title="Hello (3)";
out.println(ServletUtilities.headWithTitle(title)+
"<body bgcolor=\"#fdf5e6\">\n"+
"<h1>"+title+"</h1>\n"+
"</body></html>");
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
this.doGet(request,response);
}
}
package coreservlets;
import javax.servlet.*;
import javax.servlet.http.*;
/**Some simple timesavers.Note that most are static methods.*/
public class ServletUtilities {
public static final String DOCTYPE="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "+"Transitional//EN\">\n";
public static String headWithTitle(String title){
return(DOCTYPE+"\n"+"<html>\n"+"<head><title>"+title+"</title></head>\n");
}
}
小结:是不是很麻烦呢?每次我都要调用javac filepath进行编译,编译之后再copy到tomcat下,太机械重复了,有没有简单的呢?
当然有了,这是下面要学习的内容,且看我们下篇学习吧!
http://blog.csdn.net/hankaibo/article/details/19705205
标签:
原文地址:http://www.cnblogs.com/hellowzd/p/4762739.html