zoukankan      html  css  js  c++  java
  • servlet 将输入内容通过拼接页面的方式显示出来

    <!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">
    <!-- charset="utf-8" 页面编码 -->
    
    <title>ReturnName</title>
    </head>
    <body>
    	<form action="ReturnNameServlet" method="post">
    		<input type="text" name="textname"> 
    		<input type="submit" value="提交">
    	</form>
    </body>
    </html>
    

    returnname.html

    package controller;
    
    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 ReturnNameServlet
     */
    @WebServlet("/ReturnNameServlet")
    public class ReturnNameServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    
        /**
         * Default constructor. 
         */
        public ReturnNameServlet() {
            // TODO Auto-generated constructor stub
        }
    
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    	}
    
    	/**
    	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		
    		request.setCharacterEncoding("utf-8");
    		response.setCharacterEncoding("utf-8");
    		//设置编码字符集
    		
    		
    		String username=request.getParameter("textname");
    		response.setContentType("text/html");
    		PrintWriter out = response.getWriter();
    		out.print("<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body>");
    		out.print("<body>submitname is ");
    		out.print(username);
    		out.print("</body>");
    		out.flush();
    		out.close();
    	}
    
    }
    

     ReturnNameServlet.java

  • 相关阅读:
    java前三章总结
    Java入门第二章
    MYSQL 3306设置允许外网访问
    Lnmp Laravel搭建网站需要注意的几点:
    Linux 如何删除网站目录下的user.ini
    Linux Composer的安装
    lnmp切换PHP版本
    解决GitHub的raw.githubusercontent.com无法连接问题
    Linux 7系列默认不能连网的解决办法
    lnmp安装
  • 原文地址:https://www.cnblogs.com/tianhao/p/4020735.html
Copyright © 2011-2022 走看看