THE FOURTY-SEVENTH DAY

    今天星期二,参加培训的第四十七天,没有什么想说的话,java思想看到了多态,然后jsdom看到了最后一章,然后没有别的什么了,老师今天讲的是severlet,tomcat什么的。

    tomcat 端口设置80,就可以不用端口号在浏览器上访问了,因为浏览器默认端口就是80,然后配置xml文件 ,访问servlet,别的是真的没有什么了,作业没有做懒的去做什么,好吧,也没有什么好讲的,因为本就是这样,没有什么了,贴点代码吧:

   xml配置:

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>180424</display-name><!--   工程名 -->
  <welcome-file-list>  
    <welcome-file>web/test.html</welcome-file><!-- 默认访问文件 -->
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
   <servlet-name>TestSeverlet</servlet-name><!-- 自己定义的名字 -->
    <servlet-class>severlet.TestSeverlet</servlet-class><!--全类名 -->
    <init-param>
    <param-name>username</param-name><!-- servlet内init()方法用到的 -->
    <param-value>ttttt</param-value>
    </init-param>
    <init-param>
    <param-name>password</param-name>
    <param-value>lizean</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
  <servlet-name>TestSeverlet</servlet-name><!-- 自己定义的名字 -->
  <url-pattern>/TestSeverlet</url-pattern><!-- action后加的名字,与xx.jsp相同 -->
  </servlet-mapping>
</web-app> 

servlet:

package severlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TestSeverlet
 */
//@WebServlet("/TestSeverlet")
public class TestSeverlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestSeverlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter write = response.getWriter();
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		String name= request.getParameter("username");
		String password = request.getParameter("password");
		Enumeration<String> names=request.getParameterNames();
		while(names.hasMoreElements()) {
			String str = names.nextElement();
		}
		request.getParameterValues("interest");//复选框   选几个就是几个
		Map<String,String[]> map=request.getParameterMap();
//		write.write();
//		write.append("<!DOCTYPE html >\r\n" + 
//				"<html>\r\n" + 
//				"<head>\r\n" + 
//				"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n" + 
//				"<title>Insert title here</title>\r\n" + 
//				"</head>\r\n" + 
//				"<body>\r\n" + 
//				"22222\r\n" + name+password+"百奔跑"+
//				"</body>\r\n" + 
//				"</html>");
		request.setAttribute("name", name);
		request.getRequestDispatcher("web/test1.html").forward(request, response);
	}

	/**
	 * @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);
	}
	
	//init() 方法初始化 只会调用一次
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
	}
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		String username= config.getInitParameter("username");
		Enumeration<String> paramNames=config.getInitParameterNames();//默认排序
		while(paramNames.hasMoreElements()) {
			String param=paramNames.nextElement();
			String paramValue=config.getInitParameter(param);
			System.out.println(paramValue);
		}
//		super.init(config);
	}
	
	//service 处理客户端的请求
	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.service(arg0, arg1);
	}
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
	}

}
好了,就这样吧,没有别的什么好说的了,很是无聊,没有什么话可以说,行吧,别的没有什么,就这样。结束。

猜你喜欢

转载自blog.csdn.net/aizaiee/article/details/80070850
Day