javaEE全网最全学习路线!
0.学习路线
-
JavaEE
-
JAVA Web 开发
- Servlet 处理web请求
- JSP 页面控制
- Java 传递数据
- JDBC 访问数据库的api
-
JAVA 框架开发
-
ssm
- spring mvc(web 框架,提供接口访问、restful接口等能力)
- spring (依赖注入框架,帮助你管理 Java 对象,集成一些其他的内容)
- myBatis(Java 操作数据库的框架,持久层框架,对 jdbc 的封装)
- springboot(快速启动 / 快速集成项目。不用自己管理 spring 配置,不用自己整合各种框架)
- junit 单元测试库
-
ssm
-
1. servlet
1.1如何创建servlet(有以下三种方式)
-
实现servlet接口(/src/main/java目录下新建类生命周期LifeCycle,并且实现接口Servlet,如下图:)
package demo; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebServlet; public class LifeCycle implements Servlet{ @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("Hello,servlet!"); } @Override public void destroy() { // TODO Auto-generated method stub System.out.println("servlet destory! "); } @Override public ServletConfig getServletConfig() { // TODO Auto-generated method stub return null; } @Override public String getServletInfo() { // TODO Auto-generated method stub return null; } @Override public void init(ServletConfig config) throws ServletException { // TODO Auto-generated method stub System.out.println("hello,initiated!"); } }
继承GenericServlet(创建步骤在下方Demo)
-
继承HttpServlet(**创建方式与GenericServlet基本相同,只有在填写superclass时,需要改变为javax.servlet.http.HttpServlet **)
/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); System.out.println("hello,HttpServlet!"); //测试所需要的代码 } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); System.out.println("hello,HttpServlet!"); //测试所需要的代码!!! } }
- 通过复写 doGet 和 doPost 方法(通常使用其中一个就好)
1.2.如何配置servlet(!!!两种 配置只能使用一种!!!)
-
使用注解进行配置
@WebServlet("/ExtendsHS")
-
通过配置xml文件路径(/项目名称/src/main/webapp/WEB-IN/web.xml)来配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <!-- 配置Servlet信息 --> <servlet> <!-- 配置Servlet名称,名称必须唯一 --> <servlet-name>ServletDemo</servlet-name> <!-- 配置Servlet完全路径(包名+类名) --> <servlet-class>cn.itcast.servlet.ServletDemo</servlet-class> </servlet> <!-- 配置Servlet映射(访问路径) --> <servlet-mapping> <!-- 配置Servlet名称,必须和上面的相同 --> <servlet-name>ServletDemo</servlet-name> <!-- 配置虚拟路径(访问路径) --> <url-pattern>/demo</url-pattern> </servlet-mapping> </web-app>
1.3.Demo
1.创建JavaEE项目 (如没有 Dynamic Web Project ,进入other-> web-> Dynamic Web Project即可!)
2.创建 servlet (在/src/main/java目录下右键 -> new selvclet [如没有,同理于是在other中找寻] )
继承继承GenericServlet
- 1 : 在哪个包下创建
- 2 :创建类的名称
- 3 :要继承的超类(GenericServlet,一定不要写错javax.servlet.GenericServlet )
3.配置servlet
-
通过注解配置
@WebServlet("/GS")
-
通过xml文件配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <!-- 配置Servlet信息 --> <servlet> <!-- 配置Servlet名称,名称必须唯一 --> <servlet-name>GS</servlet-name> <!-- 配置Servlet完全路径(包名+类名) --> <servlet-class>demo.GS</servlet-class> </servlet> <!-- 配置Servlet映射(访问路径) --> <servlet-mapping> <!-- 配置Servlet名称,必须和上面的相同 --> <servlet-name>GS</servlet-name> <!-- 配置虚拟路径(访问路径) --> <url-pattern>/GS</url-pattern> </servlet-mapping> </web-app>
4. 启动tomcat服务器(在启动之前将项目加入tomcat)
- 选中tomcat服务器右击 -> 选择Add and remove -> 选中左边方框内的项目点击Add(添加至右边)-> Fishion -> 选中tomcat服务器点击start the sever
5. 修改文件,方便测试
1.**GS.class文件()GenericServlet的修改**
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("hello, GenericServlet!"); //添加这一行!!!!!!
}
2.**HS.class文件(HttpServlet)的修改**
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//注释此行!!!response.getWriter().append("Served at: ").append(request.getContextPath());
System.out.println("hello,HttpServlet!"); //添加此行代码!!!
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
System.out.println("hello,HttpServlet!");//添加此行代码!!!
}
3.**LF.class文件(生命周期)的修改**
@Override //调用 Tomcat 容器,响应请求
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Hello,servlet!");// !!!添加此行代码
}
@Override //生命周期销毁
public void destroy() {
// TODO Auto-generated method stub
System.out.println("servlet destory! "); //!!!添加此行代码
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override //生命周期初始化
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println("hello,initiated!"); //!!!添加此行代码
}
6.打开浏览器输入网址进行测试
http://localhost:8080/demo01(此处是项目名称)/GS(根据自己的配置输入这个路径)
7.返回tomcat查看是否有相应的输出,如有输出,则表示成功!