ݺߣ

ݺߣShare a Scribd company logo
Курс по Java, 2016
Servlet, JSP
Курс по Java, 2016
Servlet API
Курс по Java, 2016
javax.servlet.Servlet
import javax.servlet.GenericServlet;
public class MyServlet extends GenericServlet {
@Override
public void service(ServletRequest servletRequest,
ServletResponse servletResponse)
throws ServletException, IOException {
// do something
}
}
Курс по Java, 2016
javax.servlet.ServletRequest
String remoteAddress = servletRequest.getRemoteAddr();
String contentType = servletRequest.getContentType();
InputStream input = servletRequest.getInputStream();
String parameter = servletRequest.getParameter("name");
Object attribute = servletRequest.getAttribute("name");
servletRequest.setAttribute("name", attribute);
Курс по Java, 2016
javax.servlet.ServletResponse
servletResponse.setContentType("text/html");
servletResponse.setContentLength(256);
OutputStream output = servletResponse.getOutputStream();
Курс по Java, 2016
javax.servlet.http.HttpServlet
public class MyHttpServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// process GET request
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// process POST request
}
Курс по Java, 2016
javax.servlet.http.HttpServletRequest
String remoteAddress = request.getRemoteAddr();
String contentType = request.getContentType();
Reader reader = request.getReader();
String parameter = request.getParameter("name");
Object attribute = request.getAttribute("name");
request.setAttribute("name", attribute);
Cookie[] cookies = request.getCookies();
String proxiedClient = request.getHeader("X-Forwarded-For");
HttpSession session = request.getSession();
Курс по Java, 2016
javax.servlet.http.HttpSession
HttpSession session = request.getSession();
Object sessionAttribute = session.getAttribute("name");
session.setAttribute("name", sessionAttribute);
Курс по Java, 2016
javax.servlet.http.HttpServletResponse
response.setContentType("text/html");
response.setContentLength(256);
Writer writer = response.getWriter();
response.addCookie(new Cookie("name", "value"));
response.addHeader("X-My-Header", "value");
response.sendError(500, "Everything failed");
response.sendRedirect("/other-path");
Курс по Java, 2016
Servlet Container
Курс по Java, 2016
Apache Tomcat
Самый популярный Servlet/JSP контейнер
https://tomcat.apache.org/
Курс по Java, 2016
$CATALINA_HOME
├── bin
├── conf
├── lib
├── logs
├── webapps
│ ├── hello
│ │ ├── index.jsp
│ │ ├── META-INF
│ │ └── WEB-INF
│ │ ├── classes
│ │ │ └── it
│ │ │ └── sevenbits
│ │ │ └── example
│ │ │ └── hello
│ │ │ └── HelloWorldServlet.class
│ │ └── web.xml
│ ├── hello.war
│ └── ROOT
└── work
Курс по Java, 2016
WAR
Web Application aRchive
│ ├── hello.war
│ │ ├── index.jsp
│ │ ├── META-INF
│ │ └── WEB-INF
│ │ ├── classes
│ │ │ └── it
│ │ │ └── sevenbits
│ │ │ └── example
│ │ │ └── hello
│ │ │ └── HelloWorldServlet.class
│ │ └── web.xml
Курс по Java, 2016
War в Maven
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>it.sevenbits.example</groupId>
<artifactId>hello</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>hello Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<finalName>hello</finalName>
</build>
</project>
Курс по Java, 2016
Webapp в Maven
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
Курс по Java, 2016
Webapp в Maven
Курс по Java, 2016
web.xml
<web-app>
<display-name>Sample Web Application</display-name>
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>
it.sevenbits.example.hello.HelloWorldServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/world</url-pattern>
</servlet-mapping>
</web-app>
Курс по Java, 2016
Сборка веб приложения
mvn package
cp target/hello.war $CATALINA_HOME/webapps
Курс по Java, 2016
HelloWorldServlet
public class HelloWorldServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println("Hello, world");
}
}
Курс по Java, 2016
JSP
Java Server Pages
Servlet наоборот
Курс по Java, 2016
hello_world.jsp
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>
Курс по Java, 2016
Model — View — Controller
MVC
Курс по Java, 2016
Model
● Атрибуты запроса
● Сессия
Курс по Java, 2016
View
JSP
<!DOCTYPE html>
<html>
<head>
<title>Hello <%=request.getAttribute("name")%></title>
</head>
<body>
<h1>Hello, <%=request.getAttribute("name")%>!</h1>
</body>
</html>
Курс по Java, 2016
Controller
Servlet
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// model manipulations
String name = request.getParameter("name");
request.setAttribute("name", name);
// forward to view
RequestDispatcher dispatcher =
request.getRequestDispatcher("hello_name.jsp");
dispatcher.forward(request, response);
}
Курс по Java, 2016
Forward RequestDispatcher.forward(request, response)
Курс по Java, 2016
Redirect HttpServlerResponse.sendRedirect(“path”)
Курс по Java, 2016
Курс по Java, 2016
J2EE
Курс по Java, 2016
Домашнее задание
Поставить Tomcat.
Сделать простое веб приложение с отправкой формы.
Посмотреть, в какой java код превращаются JSP страницы.
annie.tarasenko@7bits.it denis.nelubin@7bits.it

More Related Content

Курс Java-2016. Занятие 11. Servlets, JSP

  • 1. Курс по Java, 2016 Servlet, JSP
  • 2. Курс по Java, 2016 Servlet API
  • 3. Курс по Java, 2016 javax.servlet.Servlet import javax.servlet.GenericServlet; public class MyServlet extends GenericServlet { @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { // do something } }
  • 4. Курс по Java, 2016 javax.servlet.ServletRequest String remoteAddress = servletRequest.getRemoteAddr(); String contentType = servletRequest.getContentType(); InputStream input = servletRequest.getInputStream(); String parameter = servletRequest.getParameter("name"); Object attribute = servletRequest.getAttribute("name"); servletRequest.setAttribute("name", attribute);
  • 5. Курс по Java, 2016 javax.servlet.ServletResponse servletResponse.setContentType("text/html"); servletResponse.setContentLength(256); OutputStream output = servletResponse.getOutputStream();
  • 6. Курс по Java, 2016 javax.servlet.http.HttpServlet public class MyHttpServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // process GET request } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // process POST request }
  • 7. Курс по Java, 2016 javax.servlet.http.HttpServletRequest String remoteAddress = request.getRemoteAddr(); String contentType = request.getContentType(); Reader reader = request.getReader(); String parameter = request.getParameter("name"); Object attribute = request.getAttribute("name"); request.setAttribute("name", attribute); Cookie[] cookies = request.getCookies(); String proxiedClient = request.getHeader("X-Forwarded-For"); HttpSession session = request.getSession();
  • 8. Курс по Java, 2016 javax.servlet.http.HttpSession HttpSession session = request.getSession(); Object sessionAttribute = session.getAttribute("name"); session.setAttribute("name", sessionAttribute);
  • 9. Курс по Java, 2016 javax.servlet.http.HttpServletResponse response.setContentType("text/html"); response.setContentLength(256); Writer writer = response.getWriter(); response.addCookie(new Cookie("name", "value")); response.addHeader("X-My-Header", "value"); response.sendError(500, "Everything failed"); response.sendRedirect("/other-path");
  • 10. Курс по Java, 2016 Servlet Container
  • 11. Курс по Java, 2016 Apache Tomcat Самый популярный Servlet/JSP контейнер https://tomcat.apache.org/
  • 12. Курс по Java, 2016 $CATALINA_HOME ├── bin ├── conf ├── lib ├── logs ├── webapps │ ├── hello │ │ ├── index.jsp │ │ ├── META-INF │ │ └── WEB-INF │ │ ├── classes │ │ │ └── it │ │ │ └── sevenbits │ │ │ └── example │ │ │ └── hello │ │ │ └── HelloWorldServlet.class │ │ └── web.xml │ ├── hello.war │ └── ROOT └── work
  • 13. Курс по Java, 2016 WAR Web Application aRchive │ ├── hello.war │ │ ├── index.jsp │ │ ├── META-INF │ │ └── WEB-INF │ │ ├── classes │ │ │ └── it │ │ │ └── sevenbits │ │ │ └── example │ │ │ └── hello │ │ │ └── HelloWorldServlet.class │ │ └── web.xml
  • 14. Курс по Java, 2016 War в Maven <project> <modelVersion>4.0.0</modelVersion> <groupId>it.sevenbits.example</groupId> <artifactId>hello</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>hello Maven Webapp</name> <url>http://maven.apache.org</url> <build> <finalName>hello</finalName> </build> </project>
  • 15. Курс по Java, 2016 Webapp в Maven <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> </dependencies>
  • 16. Курс по Java, 2016 Webapp в Maven
  • 17. Курс по Java, 2016 web.xml <web-app> <display-name>Sample Web Application</display-name> <servlet> <servlet-name>HelloWorldServlet</servlet-name> <servlet-class> it.sevenbits.example.hello.HelloWorldServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorldServlet</servlet-name> <url-pattern>/world</url-pattern> </servlet-mapping> </web-app>
  • 18. Курс по Java, 2016 Сборка веб приложения mvn package cp target/hello.war $CATALINA_HOME/webapps
  • 19. Курс по Java, 2016 HelloWorldServlet public class HelloWorldServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/plain"); PrintWriter out = response.getWriter(); out.println("Hello, world"); } }
  • 20. Курс по Java, 2016 JSP Java Server Pages Servlet наоборот
  • 21. Курс по Java, 2016 hello_world.jsp <!DOCTYPE html> <html> <head> <title>Hello</title> </head> <body> <h1>Hello, world!</h1> </body> </html>
  • 22. Курс по Java, 2016 Model — View — Controller MVC
  • 23. Курс по Java, 2016 Model ● Атрибуты запроса ● Сессия
  • 24. Курс по Java, 2016 View JSP <!DOCTYPE html> <html> <head> <title>Hello <%=request.getAttribute("name")%></title> </head> <body> <h1>Hello, <%=request.getAttribute("name")%>!</h1> </body> </html>
  • 25. Курс по Java, 2016 Controller Servlet protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // model manipulations String name = request.getParameter("name"); request.setAttribute("name", name); // forward to view RequestDispatcher dispatcher = request.getRequestDispatcher("hello_name.jsp"); dispatcher.forward(request, response); }
  • 26. Курс по Java, 2016 Forward RequestDispatcher.forward(request, response)
  • 27. Курс по Java, 2016 Redirect HttpServlerResponse.sendRedirect(“path”)
  • 29. Курс по Java, 2016 J2EE
  • 30. Курс по Java, 2016 Домашнее задание Поставить Tomcat. Сделать простое веб приложение с отправкой формы. Посмотреть, в какой java код превращаются JSP страницы. annie.tarasenko@7bits.it denis.nelubin@7bits.it