本篇博客是为了新手,基本上都是图,用于记录创建Servlet工程,如果已经知道如何创建工程可以关闭浏览器了
一、创建一个父工程创建maven工程,不用勾选 Create from archtype,然后一路下一步
二、创建一个子module并添加web依赖
在子工程中添加依赖
三、Servlet创建与配置javax.servlet javax.servlet-api3.0.1
public class MvcDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("hello Servlet");
System.out.println(req.getParameter("hello"));
req.getSession().setAttribute("helloContent", "This is a mvc demo. ooo");
resp.setStatus(HttpServletResponse.SC_OK);
String location = req.getContextPath() + "/hello2.jsp"; //在web目录中
resp.sendRedirect(location); //重定向
//req.getRequestDispatcher("/WEB-INF/hello.jsp").forward(req, resp); //转发
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
注意代码中:
1)重定向和转发设置路径区别!
2)WEB-INF下面的页面只能通过转发方式访问,重定向和直接浏览器访问都不可以!
hello.jsp和hello2.jsp内容一样:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
${helloContent}
四、配置Tomcat
五、总结
基于idea开发mvc程序(springmvc)基本流程就是这样的,还是比较简单



