博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编写一个简单的JAVA WEB Servlet页面
阅读量:5102 次
发布时间:2019-06-13

本文共 2812 字,大约阅读时间需要 9 分钟。

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(""); 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 WEB开发环境搭建,请看:

转载于:https://www.cnblogs.com/xieyuan/p/3787505.html

你可能感兴趣的文章
C语言课程学习的总结
查看>>
BZOJ 1304: [CQOI2009]叶子的染色
查看>>
POJ 2914 Minimum Cut
查看>>
数据库设计的三大范式
查看>>
数组元素的填充与替换、排序和复制
查看>>
gdb中信号的处理[转]
查看>>
学习Javascript闭包(Closure)
查看>>
LeetCode【709. 转换成小写字母】
查看>>
toString()和toLocaleString()有什么区别
查看>>
【mybatis】学习笔记之conf.xml与mapper.xml配置
查看>>
Python基础学习Day3 数据类型的转换、int、str、bool、字符串的常用方法、for循环...
查看>>
Controller比较两个对象discs、outlets中的元素是否相等。相同则相应的checkbox为checked...
查看>>
Android中在布局中写ViewPager无法渲染出来的问题
查看>>
简单shellcode编写
查看>>
centos7配置yum源
查看>>
反射实例化不同类型的实例
查看>>
servletConfig和ServletContext 以及servletContextListener介绍
查看>>
20175236 2018-2019-2 《Java程序设计》第六周学习总结
查看>>
小数据池.深浅拷贝.集合
查看>>
??,int?
查看>>