package com.helloweenvsfei.firstweb;
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 HelloServlet
*/
@WebServlet("/hello")
public class HelloServlet 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 {
request.setCharacterEncoding("UTF-8");//设置request的编码方式
response.setContentType("text/html;charSet=UTF-8");//设置respponse的文档类型及编码方式
PrintWriter out=response.getWriter();//获取out对象
out.println(request.getRequestURI());//输出到客户端servlet URI路径
out.println("<!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>firstWeb</title></head><body>");
out.println("<form action=‘"+request.getRequestURI()+"‘ method=‘post‘ target=‘_blank‘>");//action中如果用/hello的话就是从http"//127.0.0.1/hello与jsp不同即jsp文件中action=‘hello‘与这里out输入action=‘/aLianxi/hello‘效果相同
out.println("请输入您的名字:<input type=‘text‘ name=‘name‘/>");
out.println("<input type=‘submit‘ value=‘提交‘ />");
out.println("</form>");
String name=request.getParameter("name");
if(name!=null && name.trim().length()>0){
out.println("您好,<b>"+name+"</b>,欢迎来到Java Web世界。");
}
out.println("</body></html>");
out.flush();
out.close();
}
/**
* @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);
}
}