javaEE——javaweb基础
Posted 名字真的很急用
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaEE——javaweb基础相关的知识,希望对你有一定的参考价值。
- 网络传输协议
HTTP:两个
-
请求
-
请求行::请求的内容的第一行 : 请求的地址,请求的协议,请求的状态 请求头:一堆键值对 请求体:请求提交的数据
-
-
响应
-
响应行:响应的地址,响应的状态 响应头:一堆键值对 响应体:服务器返回来的数据
-
服务器软件
-
Tomcat
-
目录
-
bin:二进制的执行文件 cof:配置信息(端口号,默认的项目部署位置) lib:Tomcat提供的jar(类) logs:Tomcat运行日志 temp:临时文件(上传/下载) webaps:tomcat上部署项目默认路径 work:存放jsp
-
-
Tomcat:任何项目,他都默认的打开index文件
-
控制台乱码问题
-
java.util.logging.ConsoleHandler.encoding = gbk
-
拓展知识:
- JavaSE和JavaEE
- JavaSE:只能做本地操作
- 面向对象之前(API)
- JavaEE:可以处理HTTP请求和响应
- JavaSE:只能做本地操作
- 域名问题/端口号 80
idea集成Tomcat
JavaEE
- java前端开发(jsp):5
- Servlet + jsp
- Java后端开发(服务器端开发,获取数据(crud))
Servlet概述
-
接口
-
要想实现该接口,必须重写里面所有的方法
-
该类想要运行,必须要有Web服务器,Java应用程序
-
可以处理请求和响应
-
还有其他两种处理请求和响应的方式
-
.GenericServlet /HttpServlet
-
Servlet入门案例:
- 无法访问
- out目录下(web服务器)
- 该目录底下的内容,只有tomcat有访问权限,http协议是没有权限的。
- out目录下(web服务器)
Servlet方法:
-
java.lang.IllegalStateException: 启动子级时出错 ---->虚拟路径问题(/ 没配)
ServletContext:
- 代表的是整个的Java程序(由很多个Servlet组成)一个web项目下只有一个,并且它是范围最大的域对象,可以实现数据共享
Service:处理请求和响应
-
该方法只要有请求过来,容器(Servlet)会自动帮我们调用,处理请求
-
[帮我们自动的创建ServletRequest实例,并把该实例赋值给变量
-
处理请求(ServletRequest)
- 请求行 请求头 请求体
-
常用方法
-
getParameter():获取客户端提交的数据 getAttribute():获取属性值(同一个请求中的) setAttribute():设置属性值(同一个请求)
-
servlet 是运行在 Web 服务器中的小型 Java 程序。servlet 通常通过 HTTP(超文本传输协议)接收和响应来自 Web 客户端的请求。
要实现此接口,可以编写一个扩展 javax.servlet.GenericServlet 的一般 servlet,或者编写一个扩展 javax.servlet.http.HttpServlet 的 HTTP servlet。
此接口定义了初始化 servlet 的方法、为请求提供服务的方法和从服务器移除 servlet 的方法。这些方法称为生命周期方法,它们是按以下顺序调用的:
构造 servlet,然后使用 init 方法将其初始化。
处理来自客户端的对 service 方法的所有调用。
从服务中取出 servlet,然后使用 destroy 方法销毁它,最后进行垃圾回收并终止它。
Method Summary
void destroy()
Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.
ServletConfig getServletConfig()
Returns a ServletConfig object, which contains initialization and startup parameters for this servlet.
String getServletInfo()
Returns information about the servlet, such as author, version, and copyright.
void init(ServletConfig config)
Called by the servlet container to indicate to a servlet that the servlet is being placed into service.
void service(ServletRequest req, ServletResponse res)
Called by the servlet container to allow the servlet to respond to a request.
Interface ServletConfig接口
GenericServle(抽象类)实现了这个接口。
String getInitParameter(String name)
Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.
Enumeration getInitParameterNames()
Returns the names of the servlet's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.
ServletContext getServletContext()
Returns a reference to the ServletContext in which the caller is executing.
String getServletName()
Returns the name of this servlet instance
GenericServlet抽象类
public abstract class GenericServlet extends Object implements Servlet, ServletConfig
GenericServlet 实现 Servlet 和 ServletConfig 接口。servlet 可以直接扩展 GenericServlet,尽管扩展特定于协议的子类(比如 HttpServlet)更为常见。
要编写一般的 servlet,只需重写抽象 service 方法即可
void destroy()
Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.
String getInitParameter(String name)
Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.
Enumeration getInitParameterNames()
Returns the names of the servlet's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.
ServletConfig getServletConfig()
Returns this servlet's ServletConfig object.
ServletContext getServletContext()
Returns a reference to the ServletContext in which this servlet is running.
String getServletInfo()
Returns information about the servlet, such as author, version, and copyright.
String getServletName()
Returns the name of this servlet instance.
void init()
A convenience method which can be overridden so that there's no need to call super.init(config).
void init(ServletConfig config)
Called by the servlet container to indicate to a servlet that the servlet is being placed into service.
void log(String msg)
Writes the specified message to a servlet log file, prepended by the servlet's name.
void log(String message, Throwable t)
Writes an explanatory message and a stack trace for a given Throwable exception to the servlet log file, prepended by the servlet's name.
abstract void service(ServletRequest req, ServletResponse res)
Called by the servlet container to allow the servlet to respond to a request.
HttpServlet
public abstract class HttpServlet extends GenericServlet implements Serializable
Extends: GenericServlet
Implements: java.io.Serializable
所以它可以直接调用GenericServlet抽象类的方法,可以直接this.获取servletContext对象,servletConfig对象,等等。。
提供将要被子类化以创建适用于 Web 站点的 HTTP servlet 的抽象类。HttpServlet 的子类至少必须重写一个方法,该方法通常是以下这些方法之一:
doGet,如果 servlet 支持 HTTP GET 请求
doPost,用于 HTTP POST 请求
doPut,用于 HTTP PUT 请求
doDelete,用于 HTTP DELETE 请求
init 和 destroy,用于管理 servlet 的生命周期内保存的资源
getServletInfo,servlet 使用它提供有关其自身的信息
几乎没有理由重写 service 方法。service 通过将标准 HTTP 请求分发给每个 HTTP 请求类型的处理程序方法(上面列出的 doXXX 方法)来处理它们。
看源码分析为啥不用重写service方法,而是重写doget dopost方法就可以了。
因为它重载了service方法,利用向下转型把GenericServlet类中的service方法中 的参数转换为了 request = (HttpServletRequest)req;
response = (HttpServletResponse)res;这样就可以调用getMthod方法,来获取请求。
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getMethod();
long lastModified;
if (method.equals("GET")) {
lastModified = this.getLastModified(req);
if (lastModified == -1L) {
this.doGet(req, resp);
} else {
long ifModifiedSince;
try {
ifModifiedSince = req.getDateHeader("If-Modified-Since");
} catch (IllegalArgumentException var9) {
ifModifiedSince = -1L;
}
if (ifModifiedSince < lastModified / 1000L * 1000L) {
this.maybeSetLastModified(resp, lastModified);
this.doGet(req, resp);
} else {
resp.setStatus(304);
}
}
} else if (method.equals("HEAD")) {
lastModified = this.getLastModified(req);
this.maybeSetLastModified(resp, lastModified);
this.doHead(req, resp);
} else if (method.equals("POST")) {
this.doPost(req, resp);
} else if (method.equals("PUT")) {
this.doPut(req, resp);
} else if (method.equals("DELETE")) {
this.doDelete(req, resp);
} else if (method.equals("OPTIONS")) {
this.doOptions(req, resp);
} else if (method.equals("TRACE")) {
this.doTrace(req, resp);
} else {
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[]{method};
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(501, errMsg);
}
}
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
HttpServletRequest request;
HttpServletResponse response;
try {
request = (HttpServletRequest)req;
response = (HttpServletResponse)res;
} catch (ClassCastException var6) {
throw new ServletException(lStrings.getString("http.non_http"));
}
this.service(request, response);
}
private static class NoBodyAsyncContextListener implements AsyncListener {
private final HttpServlet.NoBodyResponse noBodyResponse;
public NoBodyAsyncContextListener(HttpServlet.NoBodyResponse noBodyResponse) {
this.noBodyResponse = noBodyResponse;
}
public void onComplete(AsyncEvent event) throws IOException {
this.noBodyResponse.setContentLength();
}
public void onTimeout(AsyncEvent event) throws IOException {
}
public void onError(AsyncEvent event) throws IOException {
}
public void onStartAsync(AsyncEvent event) throws IOException {
}
}
Interface ServletContext接口
它是一个域对象,可以实现数据的共享,每一个web项目只有一个ServletContext对象,范围最大。获取的参数是全局参数。
Object getAttribute(String name)
Returns the servlet container attribute with the given name, or null if there is no attribute by that name.
Enumeration getAttributeNames()
Returns an Enumeration containing the attribute names available within this servlet context.
ServletContext getContext(String uripath)
Returns a ServletContext object that corresponds to a specified URL on the server.
String getContextPath()
Returns the context path of the web application.
String getInitParameter(String name)
Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.
Enumeration getInitParameterNames()
Returns the names of the context's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the context has no initialization parameters.
RequestDispatcher getNamedDispatcher(String name)
Returns a RequestDispatcher object that acts as a wrapper for the named servlet.
String getRealPath(String path)
Returns a String containing the real path for a given virtual path.
RequestDispatcher getRequestDispatcher(String path)
Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.
URL getResource(String path)
Returns a URL to the resource that is mapped to a specified path.
InputStream getResourceAsStream(String path)
Returns the resource located at the named path as an InputStream object.
Set getResourcePaths(String path)
Returns a directory-like listing of all the paths to resources within the web application whose longest sub-path matches the supplied path argument.
String getServerInfo()
Returns the name and version of the servlet container on which the servlet is running.
Servlet getServlet(String name)
String getServletContextName()
Returns the name of this web application corresponding to this ServletContext as specified in the deployment descriptor for this web application by the display-name element.
Enumeration getServletNames()
Deprecated. As of Java Servlet API 2.1, with no replacement.
This method was originally defined to return an Enumeration of all the servlet names known to this context. In this version, this method always returns an empty Enumeration and remains only to preserve binary compatibility. This method will be permanently removed in a future version of the Java Servlet API.
Enumeration getServlets()
Deprecated. As of Java Servlet API 2.0, with no replacement.
This method was originally defined to return an Enumeration of all the servlets known to this servlet context. In this version, this method always returns an empty enumeration and remains only to preserve binary compatibility. This method will be permanently removed in a future version of the Java Servlet API.
void removeAttribute(String name)
Removes the attribute with the given name from the servlet context.
void setAttribute(String name, Object object)
Binds an object to a given attribute name in this servlet context
以上是关于javaEE——javaweb基础的主要内容,如果未能解决你的问题,请参考以下文章