Servlet form data


  Translation results:

A Java Servlet is a program that runs on a Web server or application server as an intermediary layer between requests from a Web browser or other HTTP client and a database or application on the HTTP server.

Using Servlets, you can collect user input from web forms, render records from a database or other sources, and dynamically create web pages.

Servlet form datasyntax

In many cases, some information needs to be passed from the browser to the web server and ultimately to the background program. The browser uses two methods to pass this information to the web server, the GET method and the POST method.

Servlet form dataexample

package cn.php.test;
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 HelloForm
 */@WebServlet("/HelloForm")
 public class HelloForm extends HttpServlet {    
     private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloForm() {
        super();
        // TODO Auto-generated constructor stub
    }    
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */    
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        
     // 设置响应内容类型        
     response.setContentType("text/html;charset=UTF-8");        
     PrintWriter out = response.getWriter();        
     String title = "使用 GET 方法读取表单数据";        
     // 处理中文        
     String name =new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");        
     String docType = "<!DOCTYPE html> \n";        
     out.println(docType +"<html>\n" +"<head><title>" + title + "</title></head>\n" +"<body bgcolor=\"#f0f0f0\">\n" +"<h1 align=\"center\">" + title + "</h1>\n" +"<ul>\n" +"  <li><b>站点名</b>:"+ name + "\n" +"  <li><b>网址</b>:"+ request.getParameter("url") + "\n" +"</ul>\n" +"</body></html>");    }    
    // 处理 POST 方法请求的方法    
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        
    doGet(request, response);    
    }}

Popular Recommendations

Home

Videos

Q&A