Java web:Ubuntu下不用IDE创建Servlet
Reference: Servlet without IDE
http://www.tutorialspoint.com/servlets/servlets-first-example.htm
Step | Action |
1 | 建立HelloWord.java位于路径/home/litao/Downloads |
2 | 在终端Terminal转到HelloWorld.java文件所在路径:/home/litao/Downloads 命令:cd /home/litao/Downloads 编译HelloWorld.java命令:javac HelloWorld.java 报错:error: package javax.servlet does not exist |
3 | 修复error:package javax.servlet does not exist 因为找不到servlet包(属于Tomcat),需要添加到CLASSPATH 打开用户环境配置文件profile命令:sudo gedit /etc/profile 添加:/opt/tomcat8/apache-tomcat-8.0.36/common/lib/servlet-api.jar到CLASSPATH |
4 | 继续编译,在路径/home/litao/Downloads下生成HelloWorld.class文件命令:javac HelloWorld.java |
5 | 开始部署servlet 新建文件夹classes,位于Tomcat安装目录子目录WEB-INF下/opt/tomcat8/apache-tomcat-8.0.36/webapps/ROOT/WEB-INF 将HelloWorld.class复制到classes文件夹 |
6 | 添加Servlet配置到web.xml文件 |
7 | 启动Tomcat 先转到Tomcatbin目录下 命令:cd/opt/tomcat8/apache-tomcat-8.0.36/bin Enter键之后,输入命令 .startup.sh 提示Tomcatstarted |
8 | 在浏览器地址框输入 |
1. 建立HelloWord.java
路径/home/litao/Downloads
code
// Import required java libraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*;
// Extend HttpServlet class public class HelloWorld extends HttpServlet { private String message;
public void init() throws ServletException { // Do required initialization message = "Hello World"; }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set response content type response.setContentType("text/html");
// Actual logic goes here. PrintWriter out = response.getWriter(); out.println("<h1>" + message + "</h1>"); } public void destroy() { // do nothing. } } |
2.转到HelloWorld.java文件所在路径:/home/litao/Downloads
命令:cd /home/litao/Downloads
编译HelloWorld.java
命令:javac HelloWorld.java
报错:error: package javax.servlet does not exist
3. 百度一下error:package javax.servlet does not exist
是因为找不到servlet包(属于Tomcat),需要添加到CLASSPATH
http://blog.sina.com.cn/s/blog_49dc9c4701008a9v.html
Tomcat8安装路径:/opt/tomcat8/apache-tomcat-8.0.36
JDK1.8安装路径:/usr/lib/jvm/jdk1.8.0_91
两种方式:
1)将servlet.jar添加到/usr/lib/jvm/jdk1.8.0_91/jre/lib(前提:jre/lib已配置到CLASSPATH中)
2)将/opt/tomcat8/apache-tomcat-8.0.36/common/lib/servlet-api.jar配置到CLASSPATH
选择第2种方式
打开用户环境配置文件profile
命令:sudo gedit /etc/profile
添加:/opt/tomcat8/apache-tomcat-8.0.36/common/lib/servlet-api.jar到CLASSPATH
关于CLASSPATH,PATH, JAVA_HOME, 参考
http://www.cnblogs.com/echomyecho/p/3334617.html
http://imthinking.iteye.com/blog/181855/
编译错误解决方法
Tomcat8安装路径
servlet-api.jar
JDK1.8安装路径及jre/lib所在位置
命令:sudo gedit /etc/profile
添加:/opt/tomcat8/apache-tomcat-8.0.36/common/lib/servlet-api.jar到CLASSPATH
#set java environment export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_91 export JRE_HOME=${JAVA_HOME}/jre exportCLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:/opt/tomcat8/apache-tomcat-8.0.36/lib/servlet-api.jar export PATH=${JAVA_HOME}/bin:$PATH |
4.继续编译,生成HelloWorld.class文件
命令:javacHelloWorld.java
5.新建文件夹classes,位于Tomcat安装目录子目录WEB-INF下/opt/tomcat8/apache-tomcat-8.0.36/webapps/ROOT/WEB-INF
将HelloWorld.class复制到classes文件夹
6.添加如下Servlet配置到web.xml文件
<servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/HelloWorld</url-pattern> </servlet-mapping> |
7. 启动Tomcat
先转到Tomcatbin目录下
命令:cd/opt/tomcat8/apache-tomcat-8.0.36/bin
Enter键之后,输入命令
.startup.sh
提示Tomcatstarted
8.在浏览器地址框输入
http://localhost:8080/HelloWorld