window7下Java环境安装记录:
一、安装Tomcat
1、下载tomcat 7.0,解压,无需安装,放置到目录:D:\apache-tomcat-7.0.90。
2、配置系统环境变量,CATALINA_BASE=D:\apache-tomcat-7.0.90,CATALINA_HOME=D:\apache-tomcat-7.0.90,在Path中新增“%CATALINA_HOME%\lib;%CATALINA_HOME%\bin”的环境变量。
3、进入D:\apache-tomcat-7.0.90\bin,执行startup启动tomcat,浏览器中输入“127.0.0.1:8080”即可查看是否成功。
二、搭建Servlet,使用Servlet实现动态web
1、使用Idea创建web Application project.
2、在src目录下new 一个servlet,起名:test.servlet.study
3、重写Servlet类里的doPost和doGet方法。
package test.servlet.study;
import java.io.IOException;
import java.io.PrintWriter;
public class Servlet extends javax.servlet.http.HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("POST:Hello world!" + request.getParameter("name"));
}
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
String sss = show(request.getParameter("name"));
out.println("GET:Hello world!" + sss);
}
public String show(String name) {
return name + ",你好!!!";
}
}
4、修改web.xml增加servlet-mapping标签,增加rul地址->/servlet/test/hello。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>test.servlet.study.Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/servlet/test/hello</url-pattern>
</servlet-mapping>
</web-app>
5、打war包Build-->Build Artifacts,包名webAppTest1,放到D:\apache-tomcat-7.0.90\webapps目录下。
6、重启tomcat,访问http://127.0.0.1:8080/webAppTest1/servlet/test/hello?name=jien即可。
参考:
使用Servlet:https://www.cnblogs.com/xdp-gacl/p/3760336.html
打包:https://blog.csdn.net/walk_man_3/article/details/79404911
原文地址:http://blog.51cto.com/janephp/2146589