Context domain get data from web. Learning Notes

Source: Internet
Author: User

Web. XML configuration file
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0"    xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  <context-param>      <param-name>email</param-name>      <param-value>[email protected]</param-value>  </context-param>    <context-param>      <param-name>tel</param-name>      <param-value>13888888888888</param-value>   </context-param>
Java files
import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class webDme extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        //取得对象         ServletContext context = this.getServletContext();         //从web.xml获取元素        String email =  context.getInitParameter("email");        String tel =  context.getInitParameter("tel");        if(email != null && tel != null)        {        //如果是中文需要加上这个,否则出现乱码            response.setContentType("text/html;charset=UTF-8");            response.getWriter().write("李玟邮箱:"+email+"<br/>");            response.getWriter().write("李玟电话:"+tel+"<br/>");        }    }}
Access path

Http://localhost:8080/day04/webDme

Context domain get data from web. Learning Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.