java写一个简单网页_编写一个简单的JAVA WEB Servlet页面

package com.xieyuan;

import java.io.IOException;

import java.io.PrintWriter;

import java.net.URLEncoder;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.sun.corba.se.impl.javax.rmi.CORBA.Util;

public class MyServlet extends HttpServlet {

/**

* Constructor of the object.

*/

public MyServlet() {

super();

}

/**

* Destruction of the servlet.

*/

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}

/**

* The doGet method of the servlet.

*

* This method is called when a form has its tag value method equals to get.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

execute(request, response);

}

/**

* The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

execute(request, response);

}

private void execute(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException

{

//设置编码模式

request.setCharacterEncoding("UTF-8");

response.setCharacterEncoding("UTF-8");

//获取请求的URL

String requestUrl=request.getRequestURI();

//获得请求的方式

String method=request.getMethod();

//获得请求的参数

String param=request.getParameter("name");

//设置文档类型

response.setContentType("text/html");

PrintWriter out=response.getWriter();

out.println(""-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("");

out.println("

我的第一个Servlet页面");

out.println("

");

out.println("尊敬的"+(param==null? "用户":param)+",您使用了"+method+"方法,访问了"+requestUrl+"页面");

out.println("

");

out.println("");

out.println("


");

out.println("

");

out.println("");

out.println("


");

out.println("
您的IP:"+request.getRemoteAddr()+",服务器信息:"+this.getServletContext().getServerInfo()+

",上次访问页面为:"+request.getHeader("referer")+"");

out.println("");

out.println("");

out.flush();

out.close();

}

/**

* Initialization of the servlet.

*

* @throws ServletException if an error occurs

*/

public void init() throws ServletException {

}

}

你可能感兴趣的:(java写一个简单网页)