JAVAWEB servlet的小例子

JSP:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	hello
	<form action="test" method="get">
		<input type="text" name="username">
		<input type="submit">
	</form>
</body>
</html>

 Servlet:

package com.cs.helloworld;
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;
@WebServlet("/test")
public class TestHttpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;     
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("username");
		System.out.println(name);				
		PrintWriter pw = response.getWriter();
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		
		pw.print("name:"+name);	
		String AythType = request.getAuthType();
		System.out.println("AythType"+AythType);	
		//String getContextPath()
		String ContextPath = request.getContextPath();
		System.out.println("ContextPath"+ContextPath);		
		String QueryString = request.getQueryString();
		System.out.println("QueryString"+QueryString);
		String method = request.getMethod();
		System.out.println(method);
	}
}

结果如下:

name:xiaoshideyu
AythType:null
ContextPath:/FirstHello
QueryString:username=xiaoshideyu
methodGET

 

posted @ 2016-10-30 20:44  迷失的鱼  阅读(3006)  评论(0编辑  收藏  举报