对于初学j2ee的人来说,是非常困难的,因为我们要学的东西太多太多,这些技术点就像一盘散沙,总感觉学了忘,忘了学,我们要学html,css,js,sql,jsp,xml,当然,这些学起来是没有什么难度的,难的是在实际工作中得到应用,刚才我说的只是一些必须要掌握的技术点,那我们还要面对各种各样的开源流行框架,有些框架是必须掌握的,像,bootstrap,jquery,easyUI,ssh,spring MVC,MyBatis3等等吧,我们看到这些有时候脑袋都大了。那么我给大家的建议就是不要看这些框架,基础打好,从最基本的开始一点一点做起,说到这里,j2ee必须要从servlet和filter说起。
下面写一个最最简单的web项目。
在eclipse中创建一个dynamic web project,其目录结构:
非常简单,就两个java文件,一个servlet一个filter,然后在web.xml中做了一下配置,web.xml中的代码:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>HelloWorld</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>HellowordServlet</servlet-name> <servlet-class>com.gefufeng.web.HellowordServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HellowordServlet</servlet-name> <url-pattern>/helloword</url-pattern> </servlet-mapping> <filter> <filter-name>HelloworldFilter</filter-name> <filter-class>com.gefufeng.filter.HelloWorldFilter</filter-class> </filter> <filter-mapping> <filter-name>HelloworldFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
servlet的配置说明当我门请求http://localhost:8080/HelloWorld/helloword 时映射到
<servlet-mapping> <servlet-name>HellowordServlet</servlet-name> <url-pattern>/helloword</url-pattern> </servlet-mapping>
然后又找到了
<servlet> <servlet-name>HellowordServlet</servlet-name> <servlet-class>com.gefufeng.web.HellowordServlet</servlet-class> </servlet>
最后执行HelloWorldServlet,看一下HelloWorldServlet里的代码:
package com.gefufeng.web; 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 HellowordServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("gbk"); resp.getWriter().print("你好,世界"); } }
是不是非常简单,这里呢因为我要在页面上输出中文,改了下编码,然后项目添加到Tomcat中,运行,然后http://localhost:8080/HelloWorld/helloword 你会发现
然后在filter作为一个过滤器,代码为:
package com.gefufeng.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; public class HelloWorldFilter implements Filter{ public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest htRequest = (HttpServletRequest) servletRequest; String path = htRequest.getServletPath(); System.out.println(path); filterChain.doFilter(servletRequest, servletResponse); } public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
这里我只是让他在控制台打印出了地址,然后继续运行。
好了,demo就这么简单,j2ee学习从这里开始吧。
源码链接http://www.oschina.net/code/snippet_2270118_54505