zoukankan      html  css  js  c++  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. <br>
    	 */
    	public void destroy() {
    		super.destroy(); // Just puts "destroy" string in log
    		// Put your code here
    	}
    
    	/**
    	 * The doGet method of the servlet. <br>
    	 *
    	 * 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. <br>
    	 *
    	 * 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("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
    		out.println("<HTML>");
    		out.println("<HEAD><TITLE>我的第一个Servlet页面</TITLE></HEAD>");
    		out.println("<BODY>");
    		out.println("尊敬的"+(param==null? "用户":param)+",您使用了"+method+"方法,访问了"+requestUrl+"页面");
    		out.println("<form action='"+requestUrl+"' method='get' >");
    		out.println("<input type='text' name='name' /><input type='submit' value='GET提交' />");
    		out.println("</form><BR/>");
    		
    		out.println("<form action='"+requestUrl+"' method='post' >");
    		out.println("<input type='text' name='name' /><input type='submit' value='POST提交' />");
    		out.println("</form><BR/>");
    		
    		
    		out.println("<BR/>您的IP:"+request.getRemoteAddr()+",服务器信息:"+this.getServletContext().getServerInfo()+
    				",上次访问页面为:"+request.getHeader("referer")+"<BR/><script>document.write('本页面最后更新时间:'+document.lastModified);</script>");
    		out.println("</BODY>");
    		out.println("</HTML>");
    		out.flush();
    		out.close();
    	}
    	
    	/**
    	 * Initialization of the servlet. <br>
    	 *
    	 * @throws ServletException if an error occurs
    	 */
    	public void init() throws ServletException {
    	}
    
    }
    


    JAVA WEB开发环境搭建,请看:http://blog.csdn.net/tabactivity/article/details/11097783



  • 相关阅读:
    ValidateInput(false)与this.ValidateRequest = false无效的解决方案
    WPF ListView CellTemplate Border设置ListView单元格的边框
    MFC4简单的窗口重绘(非部分重绘)
    针对每个用户的作业(针对用户的定期事件)
    用JQUERY做大表单(多表单域)AJAX提交
    MFC2消息处理
    asp.net mvc实战学习笔记(1)
    NHibernate基础拾遗
    FLEX/FLASH冒泡事件与非冒泡事件
    MFC1创建窗体
  • 原文地址:https://www.cnblogs.com/xieyuan/p/3787505.html
Copyright © 2011-2022 走看看