javaweb

本文介绍了Servlet的基础知识,包括如何解决HTTP响应乱码问题,实现请求重定向,以及如何使用HttpServletReques对象获取请求行、请求列和消息头。此外,还展示了请求转发和定时刷新页面的实现方法,为Web开发提供了基础指南。
摘要由CSDN通过智能技术生成

一.学习内容
(1)解决响应乱码问题
提供解决乱码的方案:response.setContentType(“text/html;charset=utf-8”);
(2)实现网页定时刷新跳转
(3)HttpServletRedquest对象
获取请求行
获取请求列
获取请求消息头
(4)请求转发
(5)实现请求重定向
二.代码如下:
(1)解决响应乱码

package Demo;

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

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 ChineseServlet
 */
@WebServlet({ "/ChineseServlet", "/chinese" })
public class ChineseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		String data="中国-大数据产业";
		System.out.println("chinses 收到前端请求,打印大数据");
		PrintWriter pw=response.getWriter();
		pw.println(data);
		
	}

	/**
	 * @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);
	}

}

(2)实现请求重定向

package Demo;

import java.io.IOException;
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 LoginServlet
 */
@WebServlet({ "/LoginServlet", "/login" })
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("前端请求login,而不是logindashuju.html");
		response.sendRedirect("/day_10.21/logindashuju.html");
	}

	/**
	 * @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);
	}

}

(3)消息头

package Demo;

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

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 RedquestHeaderServlet
 */
@WebServlet( "/requesthead ")
public class RedquestHeaderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		Enumeration<String> er=request.getHeaderNames();
		while(er.hasMoreElements()) {
			String name=er.nextElement();
			String value =request.getHeader(name);
			response.getWriter().print("消息头:"+name+""+value);//送回网页前端
		    System.out.println("消息头:"+name+""+value);//在服务器后端打印
		}
	}

	/**
	 * @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);
	}

}

(4)消息行

package Demo;

import java.io.IOException;
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 RedquestLineServlet
 */
@WebServlet({ "/tequestline" })
public class RedquestLineServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method =request.getMethod();
		System.out.println("请求方式:"+method);
		
		String proto=request.getProtocol();
		System.out.println("请求协议:"+proto);
		
		String contextPath =request.getContextPath();
		System.out.println("项目路径:"+contextPath);
		
		String servlePath=request.getServletPath();
		System.out.println("servlet路径:"+servlePath);
		
		String pathURI =request.getRequestURI();
		StringBuffer pathURL=request.getRequestURL();
		System.out.println("URI"+pathURI+"URL"+pathURL);
		
	}

	/**
	 * @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);
	}

}

(5)定时刷新跳转

package Demo;

import java.io.IOException;
import java.util.Date;

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 RefreshServlet
 */
@WebServlet({ "/RefreshServlet", "/refresh" })
public class RefreshServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
static int num =0;
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		System.out.println("收到请求" +num);
		response.setHeader("Refresh", "5");
		response.getWriter().print(new Date());
		num++;
	}

	/**
	 * @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);
	}

}

(6)获取请求参数

package Demo;

import java.io.IOException;
import java.util.Arrays;

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 RequestParamServlet
 */
@WebServlet({ "/RequestParamServlet", "/RequestParam" })
public class RequestParamServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("收到前端的请求");
		String username=request.getParameter("username");
		String ps=request.getParameter("password");
		System.out.println("用户名:"+username);
		System.out.println("密码:"+ps);
		String [] hobbys=request.getParameterValues("hobby");
		System.out.println("hobby:"+Arrays.toString(hobbys));
	}

	/**
	 * @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);
		request.setCharacterEncoding("utf-8");
		String username=request.getParameter("username");
		System.out.println(username);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值