JSP/Servlet环境配置
- 下载安装JDK并配置环境变量
这里我下载安装的是jdk-7u51-windows-i586,如果你没有请到Oracle官网下载;
【我的电脑】-【高级系统设置】-【环境变量】-系统变量中的Path添加Java bin目录;
命令窗口测试配置环境变量正确与否。 - 下载服务器
我学习使用的是Web容器apache-tomcat-7.0.50-windows-x86,如果没有请到Apache下载。 - 配置服务器
确定SDK安装目录,即JAVA_HOME环境变量,这一步应该在第一步中已经配置;
指定端口,默认是8080,如果你不喜欢用它,或者该端口被占用,应该指定为其它;
执行startup.bat启动Tomcat,在浏览器中测试。 - 建立开发环境
创建开发目录,我这里的为D:\project\MyServlet\src
设置CLASSPATH,主要是为了告诉编译器Servlet类的位置,值为“.;D:\project\MyServlet\src;D:\tools\apache-tomcat-7.0.50\lib\servlet-api.jar”,配置如下:
创建快捷方式,方便测试时快速启动和关闭Tomcat。 - 测试系统环境变量
检查服务器的基本配置
将helloworld.html和helloworld.jsp放置到<CATALINA_HOME>/webapps/ROOT下
helloworld.html- <span style="font-size: 18px;"><html>
- <head>
- <title>test</title>
- </head>
- <body>
- Hello world
- </body>
- </html></span>
<html>
<head>
<title>test</title>
</head>
<body>
Hello world
</body>
</html>helloworld.jsp
- <span style="font-size: 18px;"><!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></span>
<!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>浏览器访问:
测试不使用包的Servlet
在命令窗口下编码HelloServlet.java,将编译后的class文件放到<CATALINA_HOME>/webapps/ROOT/WEB-INF/class下
HelloServlet.java- <span style="font-size: 18px;">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);
- }
- }</span>
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
因为我们使用的是tomcat7,无法使用其调用器invoker,所以要手动添加映射,打开<CATALINA_HOME>/webapps/ROOT/WEB-INF下的web.xml,编辑如下:- <span style="font-size: 18px;"><?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>
- </span>
<?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,在浏览器访问,如下,则说明正确
测试使用的Servlet
带包的HelloServlet2.java如下:- <span style="font-size: 18px;">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);
- }
- }</span>
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);
}
}像上面一样,编译之后就class文件拷贝到<CATALINA_HOME>/webapps/ROOT/WEB-INF/class/coreservlets包下,并在web.xml文件中增加如下内容
- <span style="font-size: 18px;"><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></span>
<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>在这里要注意,web.xml中,引用HelloServlet2时,包与class文件之间的分隔符是圆点“.";classes下的包名要与package coreservlets一致,否则,你懂的。
重启tomcat,在浏览器测试,如下测试使用包和实用工具类的Servlet
HelloServlet3.java- <span style="font-size: 18px;">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);
- }
- }</span>
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);
}
}ServletUtilities.java
- <span style="font-size: 18px;">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");
- }
- }
- </span>
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");
}
}剩下的就是编译,拷贝,修改web.xml,重启Tomcat,最后在浏览器中测试如下:
小结:是不是很麻烦呢?每次我都要调用javac filepath进行编译,编译之后再copy到tomcat下,太机械重复了,有没有简单的呢?
当然有了,这是下面要学习的内容,且看我们下篇学习吧!
http://blog.csdn.net/hankaibo/article/details/19705205
JSP/Servlet环境配置的更多相关文章
- Eclipse JSP/Servlet 环境搭建
Eclipse JSP/Servlet 环境搭建 本文假定你已安装了 JDK 环境,如未安装,可参阅 Java 开发环境配置. 我们可以使用 Eclipse 来搭建 JSP 开发环境,首先我们分别下载 ...
- JSP-Runood:Eclipse JSP/Servlet 环境搭建
ylbtech-JSP-Runood:Eclipse JSP/Servlet 环境搭建 1.返回顶部 1. Eclipse JSP/Servlet 环境搭建 本文假定你已安装了 JDK 环境,如未安装 ...
- Java学习笔记之:Java Servlet环境配置
一.介绍 Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层. 使用 ...
- JSP开发环境配置问题解答
有过JSP开发经验的同学对于JSP开发环境的配置一定非常的很有感触,十分的繁琐,有时因为一个小的问题导致我们配置的配置前功尽弃,本篇我将重点带领大家一起探讨一下关于JSP环境配置的一些常见问题,及解决 ...
- jsp/servlet环境搭建
手动配置servlet开发环境: 1. eclipse.tomcat.jdk下载安装: 2. eclipse新建项目,项目依赖tomcat的jar包(包含tomcat和servlet相关jar包)以及 ...
- JSP servlet的配置与使用
1. servlet 的配置文件内容如下所示 <servlet> <description>This is the description of my J2EE com ...
- servlet:从入门到实战学习(1)---全·环境配置
最近公司忙加班学习,学校忙助教工作,博客鸽了好久,后端学习工作过程中学了好多东西,趁着工作之余得空补补博客,算是整理下学习的东西. javaweb的后端研发需要学习的是tomcat+servlet+j ...
- 【Java】各种软件安装与环境配置的失败
又来到了java的世界,看了一段时间的视频.感觉太空虚,便从网上找到一个教程.想做几个demo试试,少不了的前期准备:Java开发环境配置,Eclipse JSP/Servlet 环境搭建等. ...
- JSP-Runoob:JSP开发环境搭建
ylbtech-JSP-Runoob:JSP开发环境搭建 1.返回顶部 1. JSP 开发环境搭建 JSP开发环境是您用来开发.测试和运行JSP程序的地方. 本节将会带您搭建JSP开发环境,具体包括以 ...
随机推荐
- Apache -Common-lang包使用
原文:http://weigang-gao.iteye.com/blog/2188739 ArrayUtils – 用于对数组的操作,如添加.查找.删除.子数组.倒序.元素类型转换等: BitFiel ...
- java_hibernate
入门:http://jingyan.baidu.com/article/cbf0e500965a352eab289368.html 步骤1.查看是否hibernate支持:file-->plug ...
- ajax回调中window.open弹出的窗口会被浏览器拦截的解决方法
存在问题:处理页面ajax请求过程中,异步请求成功后需要新开窗口打开 url,使用的是 window.open() 方法 来实现,最终都被浏览器拦截了.不会跳到对应的页面,如下 原因:浏览器之所以拦截 ...
- Vue组件进阶知识总结
上一篇我们重点介绍了组件的创建.注册和使用,熟练这几个步骤将有助于深入组件的开发.另外,在子组件中定义props,可以让父组件的数据传递下来,这就好比子组件告诉父组件:“嘿,老哥,我开通了一个驿站,你 ...
- [Python爬虫] 之二十六:Selenium +phantomjs 利用 pyquery抓取智能电视网站图片信息
一.介绍 本例子用Selenium +phantomjs爬取智能电视网站(http://www.tvhome.com/news/)的资讯信息,输入给定关键字抓取图片信息. 给定关键字:数字:融合:电视 ...
- Python学习笔记(七)函数的使用
python中的函数使用较简单,这里列出值得注意的几点: 内嵌函数 例如: # coding: utf-8 def foo(): def bar(): print 'bar() called. ...
- C++11 Lambda表达式简单解析
C++11 新增了非常多特性,lambda 表达式是当中之中的一个.假设你想了解的 C++11 完整特性, 建议去http://www.open-std.org/看看新标准! 非常多语言都提供了 la ...
- Python——调用shell命令的三种方法
1.用os.system(cmd) 不过取不了返回值 2.用os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 如a=os.popen(cmd ...
- 微信小程序 如何定义全局函数?
微信小程序 定义全局数据.函数复用.模版等 微信小程序定义全局数据.函数复用.模版等问题总结: 1.如何定义全局数据 在app.js的App({})中定义的数据或函数都是全局的,在页面中可以通过var ...
- rpm安装找不到.so库文件(linux动态库连接的相关知识)(转)
1.找不到库文件的原因 库文件不存在 这种情况一般是因为所需要的包没装,只要安装相应的包就可以解决 存在而系统不知道 这种情况一般出现在自己编译软件时候 确保库文件所在的路径已加入系统,在/etc/l ...