• Servlet:Post提交表单


    src 目录下com.xieyuan包MyServlet.java文件(Servlet文件)

    package com.xieyuan;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.net.URLEncoder;
    import java.util.Enumeration;
    import java.util.Random;
    
    import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    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;
    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;
    
    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");
    		
    		response.setContentType("text/html");
    		String getName=request.getParameter("name");
    		String getHibby=request.getParameter("hobby");
    		
    		PrintWriter out=response.getWriter();
    		out.println("<DOCTYPE HTML PUBLIC "-//W3C//DTD HTML4.0 Transitional//EN">");
    		out.println("<HTML>");
    		out.println("<HEAD><TITLE>结果</TITLE></HEAD>");
    		out.println("<BODY>");
    		out.println("您的信息如下:");
    		out.println(getName+"
    "+getHibby);
    		out.println("<br/><input type='button' value='返回上一页' onclick='history.go(-1)' />");
    		out.println("</BODY>");
    		out.println("</HTML>");
    	}
    	
    	/**
    	 * Initialization of the servlet. <br>
    	 *
    	 * @throws ServletException if an error occurs
    	 */
    	public void init() throws ServletException {
    	}
    
    }
    
    部署好WEB后,访问:http://127.0.0.1:8080/Test/servlet/MyServlet 进入登录页面
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.jsp' starting page</title>
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    	<meta http-equiv="description" content="This is my page">
    	<!--
    	<link rel="stylesheet" type="text/css" href="styles.css">
    	-->
    	
      </head>
      
      <body>
         <form action="servlet/MyServlet" method="post">
            <div align="center"><BR/>
            <fieldset style='80%' >
               <legend>填写信息</legend><br/>
               <div class="line" align="left">
                  姓名:<input type="text" name="name" width="200px" /><br/>
                  爱好:<input type="checkbox" name="hobby" value="踢球" id="tiqiu"/>
                      <label for="tiqiu">踢球</label>
                      <input type="checkbox" name="hobby" value="上网" id="shangwang" />
                      <label for="shangwang">上网</label>
                      
                      <input type="checkbox" name="hobby" value="下棋" id="xiaqi" />
                      <label for="xiaqi">下棋</label>
                 </div>
                      <br/><br/><br/><br/><input type="submit" value=" 提  交 ">
              
            </fieldset>
            </div>
         </form>
         <form>
    
    

  • 相关阅读:
    从运维角度浅谈 MySQL 数据库优化
    好的架构不是设计出来的,而是演进出来的
    京东咚咚架构演进
    大型网站的架构
    MySQL开发规范
    MySQL 5.6的72个新特性(译)
    MySQL数据库运维的五大指标
    MySQL5.0、5.1、5.5、5.6功能进化
    MySQL各版本的区别
    ajax该什么时候用
  • 原文地址:https://www.cnblogs.com/xieyuan/p/3787501.html
Copyright © 2020-2023  润新知