在这里插入图片描述
GenericServlet抽象类
在这里插入图片描述
在这里插入图片描述
HttpServlet
在这里插入图片描述
Servlet程序的生命周期
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
servlet编程
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
.java配置
在这里插入图片描述
在这里插入图片描述
源代码

<?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">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
    <servlet>//重视头
        <servlet-name>Hello</servlet-name>
        <servlet-class>Hello</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Hello</servlet-name>
        <url-pattern>/Hello</url-pattern>
    </servlet-mapping>//重视尾
</web-app>

.Hello.java

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Response;


public class Hello extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	PrintWriter out=resp.getWriter();
	ServletContext s=this.getServletContext();
	Integer count=(Integer) s.getAttribute("count");
	if(count==null)
	{
		s.setAttribute("count", 1);
		out.println("You Clicked 1 time");
	}
	else
	{
		s.setAttribute("count", count+1);
		out.println("You Clicked "+(count+1)+" time");
	}
	ServletContext c=this.getServletContext();
			out.println("<h1>"+c.getInitParameter("i")+"</h1>");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter out=resp.getWriter();
		out.println("post");
	}

}

请求Hello.java

<form action="Hello" method="POST">//重视
<%session.setAttribute("a",true);%>
用户名:<input name="username"/><br/>
密码:<input name="password" type="password"/><br/>
<input type="submit"/>

</form>

httpsession

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	PrintWriter out=resp.getWriter();
	HttpSession s=req.getSession();
	//ServletContext s=this.getServletContext();  //application
	Integer count=(Integer) s.getAttribute("count");
	if(count==null)
	{
		s.setAttribute("count", 1);
		out.println("You Clicked 1 time");
	}
	else
	{
		s.setAttribute("count", count+1);
		out.println("You Clicked "+(count+1)+" time");
	}
	out.println("get");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter out=resp.getWriter();
		out.println("post");
	}

}

web.xml配置参数

//web.xml
<servlet>
        <servlet-name>Hello</servlet-name>
        <servlet-class>Hello</servlet-class>
        <init-param>
        <param-name>name</param-name>
        <param-value>dunqipeng</param-value>
        </init-param>
    </servlet>

//.java
	ServletConfig c=this.getServletConfig();
			out.println(c.getInitParameter("name"));

全局配置

//web.xml,写在serverlet外面
<context-param>
  <param-name>i</param-name>
  <param-value>you</param-value>
  </context-param>

//.java
ServletContext c=this.getServletContext();
			out.println("<h1>"+c.getInitParameter("i")+"</h1>");
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐