Tomcat安装
首先找到tomcat的tomcat插件中找到把两个jar包导入ecilpse中的plugins中
Web项目的结构和其中的内容然后在windows中的reference中输入tomcat中设置该tomcat中的该tomcat中的版本和路径
Javaweb项目搭建
Web项目结构
项目名
WEB-INF
lib
classes
web.xml
Index.html
部署
两种方式
1. 把整个项目放入tomcat中的webapps然后
<Context docBase="" path=""/>中部署
web.xml中的设置
<?xml version="1.0" encoding="ISO-8859-1"?> <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_3_1.xsd" version="3.1" <!-- false和true的方式true为通过下面注解反射而false是通过注解反射 --> metadata-complete="false"> <!-- 请求路径 --> <servlet> <servlet-name>qq</servlet-name> <servlet-class>cn.jiedada.serlevt.HelloSerlvet</servlet-class> </servlet> <servlet-mapping> <servlet-name>qq</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet> <servlet-name>ff</servlet-name> <servlet-class>cn.jiedada.serlevt.LifeServer</servlet-class> <init-param> <param-name>coding</param-name> <param-value>utf-8</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>ff</servlet-name> <url-pattern>/xml</url-pattern> </servlet-mapping> </web-app>
2.通过tomcat然后复制项目名的位置就可以了
搭建项目
Servlet入门
index.html中的文件
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="forGet" method="get"> <h2>get请求</h2> 账号:<input type="text" name="name"><br></br> 密码:<input type="password" name="pwd"><br></br> <input type="submit" value="提交"> </form> <hr/> <form action="forPost" method="post"> <h2>post请求</h2> 账号:<input type="text" name="name"><br></br> 密码:<input type="password" name="pwd"><br></br> <input type="submit" value="提交"> </form> </body> </html>
1. 连接
package cn.jiedada.serlevt; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloSerlvet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("欢迎您"); } }
打印到控制台
2. 生命周期
package cn.jiedada.serlevt; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LifeServer extends HttpServlet { ServletConfig config; public LifeServer() { System.out.println("这是构造方法"); } @Override public void init() throws ServletException { System.out.println("这是一个init"); } @Override public void init(ServletConfig config) throws ServletException { this.config=config; System.out.println("这是一个config方法"); } @Override protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { System.out.println("这是主题"); System.out.println(config.getServletName()); System.out.println(config.getInitParameter("coding")); } @Override public void destroy() { System.out.println("销毁方法"); } }
3. 获得网页的get和post请求中的数据
4. 通过Java后台页面发送数据
package cn.jiedada.serlevt; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //注解也可以多个反射,通过该方法找到几种反射 @WebServlet(urlPatterns={"/forGet","/forPost"}) public class MyServiceTest extends HttpServlet { @Override protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { //通过post请求返回的文字为utf-8 arg0.setCharacterEncoding("utf-8"); System.out.println(arg0.getParameter("name")); System.out.println(arg0.getParameter("pwd")); //把Java文件中的数据输出到页面 ServletOutputStream os = arg1.getOutputStream(); //页面返回值设置 arg1.setContentType("text/html;charset=utf-8"); //字节流转换为字符流 OutputStreamWriter osw = new OutputStreamWriter(os); PrintWriter pw = new PrintWriter(osw,true); pw.println("这是一个表"); } }
5. 访问过程,先通过继承httpservlet继承,然后先寻找xml是否有该界面,然后在WEB-INF下面寻找界面
6. 注意
false和true的方式true为通过下面注解反射而false是通过注解反射 metadata-complete="false">