⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplei18nservlet.java

📁 一个java写的加密算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			// Output stream for the response			PrintWriter out		= res.getWriter();			out.println("<html>");			out.println("<head><title>Including another servlet from a servlet</title></head>");			out.println("<body>");			out.println("<br>The name from forwarding servlet : " + name );			RequestDispatcher dispatcher;			dispatcher = getServletContext().getRequestDispatcher("/ForwardedServlet");			dispatcher.forward(req, res);		} catch (Exception e) {			e.printStackTrace();		}    }    /** Send error messages using response's sendError method. The sendError method sends multi byte error messages correctly     *  based on the content type set to appropriate encoding.     *  @param req servlet request     *  @param res servlet response     */    protected void sendErrorMessage (HttpServletRequest req, HttpServletResponse res)    throws ServletException, IOException {        try {			String name			=	req.getParameter("name");			String charsetval	=	req.getParameter("charsetval");			res.setContentType("text/html; charset=" + charsetval);			res.sendError(404, "I18n error message from servlet "+ name);		} catch (Exception e) {			e.printStackTrace();		}    }	/** Load resource bundle based on client's locale. If there is no resource bundle available, the default resource bundle     *  will be picked up     *  @param req servlet request     *  @param res servlet response     */    protected void useResourceBundle (HttpServletRequest req, HttpServletResponse res)    throws ServletException, IOException {        try {			String name			=	req.getParameter("name");			String charsetval	=	req.getParameter("charsetval");			res.setContentType("text/html; charset=" + charsetval);			ResourceBundle rb	=	null;			rb					=	ResourceBundle.getBundle("LocalStrings",req.getLocale());			String msg;			PrintWriter out		=	res.getWriter();			out.println("<html>");			out.println("<head>");			try {				msg				=	rb.getString ("title");				out.println("<title>" + msg + "</title>");				out.println("</head>");				out.println("<body>");				out.println("<br><H1> Hello " + name + ", the following messages are displayed from a resource bundle</H1>");				msg = rb.getString ("msg");				out.println("<br><br><H2>" + msg + "</H2>");				msg = rb.getString ("thanks");				out.println("<br><br><H3>" + msg + "</H3>");			} catch (MissingResourceException e) {				e.printStackTrace();				} catch (Exception e) {				e.printStackTrace();        	}			out.println("<br>");			out.println("<P><BR><A HREF=\"/i18n-simple\">Back to sample home</A></P>");        	out.println("</body></html>");        	out.close();		} catch (Exception e) {			e.printStackTrace();		}    }	/** Forwards request from servlet to jsp using RequestDispatcher's forward method. Here the data display behaviour     *  changes based on the content type set. If the forwarded jsp sets the content type to a different value then the new content type     *  should be set for the output stream     *  @param req servlet request     *  @param res servlet response     */    protected void forwardJsp (HttpServletRequest req, HttpServletResponse res)    throws ServletException, IOException {        try {			RequestDispatcher dispatcher;			dispatcher = getServletContext().getRequestDispatcher("/ForwardedJsp.jsp");      		dispatcher.forward(req, res);		} catch (Exception e) {			e.printStackTrace();		}    }    /** It uses a new API introduced in servlet 2.4 to set character set encoding     *  of a response object.     *  @param req servlet request     *  @param res servlet response     */    protected void expSetResCharset (HttpServletRequest req, HttpServletResponse res)    throws ServletException, IOException {        try {			/* get locale name and encoding name which is returned in                          * "locale:charset" format.                         * locale name is used for selecting a resource bundle as well as response locale.                          * encoding name is used for setting the response chraset.                          */			String loc_enc	= req.getParameter("loc_enc_name");                        String default_loc_enc = "en:ISO_8859-1";                        if (loc_enc == null)                        {				System.out.println("No locale encoding informaiton was received.");				System.out.println("Using the default: "+default_loc_enc);                                loc_enc = default_loc_enc;                        } 		        String[] input_array = loc_enc.split(":");			int index = input_array[0].indexOf('_');			Locale loc = null;			if (index < 0){                        	loc = new Locale(input_array[0]);			}			else			{				String[] lang_country = input_array[0].split("_");				loc = new Locale(lang_country[0], lang_country[1]);			}				                        /*                          * Set response charset and response locale - new in Servlet2.4                          */	                res.setCharacterEncoding(input_array[1]);			res.setLocale(loc);                        /*                          * Use resource bundle to get localized string                          */                        String res_base="resources.ResourceString";                        ResourceBundle rb = ResourceBundle.getBundle(res_base,loc);               		String page_msg = rb.getString("pageLang");                        /*                          * Use MessageFormatter to insert a dynamic variable to the 2nd string                         * obtained through the resource bundle.                         */			MessageFormat mf = new MessageFormat("");                        mf.setLocale(loc);			mf.applyPattern(rb.getString("pageEnc"));		        Object[] msg_arg = { res.getCharacterEncoding()};                        String enc_msg = mf.format(msg_arg);			res.setContentType("text/html");			PrintWriter out = res.getWriter();			out.println("<html>");			out.println("<head>");			out.println("<title>Servlet i18n samples</title></head>");			out.println("<body>");			out.println("<P><FONT FACE=\"Times New Roman\"><FONT SIZE=6><B>Servlet i18n samples</B></FONT></FONT></P>");   			out.println("<p><FONT FACE=\"Times New Roman\"><b>Explicitly set character encoding for the response object</b></p>");   			out.println("<p>"+page_msg+"<br>"+enc_msg+"</p>");			out.println("<P><BR><FONT FACE=\"Times New Roman\"><A HREF=\"/i18n-simple\">Back to sample home</A></P>");			out.println("<p><font size=1><font face=\"verdana,arial,helvetica,sans-serif\">Copyright(c) 2004 Sun Microsystems, Inc. All rights reserved.</font></p>");			out.close();			} catch (Exception e) {				e.printStackTrace();			}    }    /** It uses a new API introduced in servlet 2.4 to set character set encoding     *  of a response object in implicity manner.     *  @param req servlet request     *  @param res servlet response     */    protected void impSetResCharset (HttpServletRequest req, HttpServletResponse res)    throws ServletException, IOException {        try {			/* get locale name and encoding name which is returned in                          * "locale.charset" format.                         * locale name is used for selecting a right resource bundle.                          * encoding name is used for setting the response chraset.                          */			String loc_name = req.getParameter("loc_name");                        String default_loc = "en";                        if (loc_name == null)                        {				System.out.println("No locale encoding informaiton was received.");				System.out.println("Using the default: "+default_loc);                                loc_name = default_loc;                        }			int index = loc_name.indexOf('_');			Locale loc = null;			if (index < 0){                        	loc = new Locale(loc_name);			}			else			{				String[] lang_country = loc_name.split("_");				loc = new Locale(lang_country[0], lang_country[1]);			}				                        /*                          * Set response charset and response locale - new in Servlet2.4                          */			res.setLocale(loc);                        /*                          * Use resource bundle to get localized string                          */                        String res_base="resources.ResourceString";                        ResourceBundle rb = ResourceBundle.getBundle(res_base,loc);               		String page_msg = rb.getString("pageLang");                        /*                          * Use MessageFormatter to insert a dynamic variable to the 2nd string                         * obtained through the resource bundle.                         */			MessageFormat mf = new MessageFormat("");                        mf.setLocale(loc);			mf.applyPattern(rb.getString("pageEnc"));		        Object[] msg_arg = { res.getCharacterEncoding()};                        String enc_msg = mf.format(msg_arg);			res.setContentType("text/html");			PrintWriter out = res.getWriter();			out.println("<html>");			out.println("<head>");			out.println("<title>Servlet i18n samples</title></head>");			out.println("<body>");			out.println("<P><FONT FACE=\"Times New Roman\"><FONT SIZE=6><B>Servlet i18n samples</B></FONT></FONT></P>");   			out.println("<p><FONT FACE=\"Times New Roman\"><b>Implicitly set character encoding for the response object</b></p>");   			out.println("<p>"+page_msg+"<br>"+enc_msg+"</p>");			out.println("<P><BR><FONT FACE=\"Times New Roman\"><A HREF=\"/i18n-simple\">Back to sample home</A></P>");			out.println("<p><font size=1><font face=\"verdana,arial,helvetica,sans-serif\">Copyright(c) 2004 Sun Microsystems, Inc. All rights reserved.</font></p>");			out.close();			} catch (Exception e) {				e.printStackTrace();			}    }    /** Handles the HTTP <code>GET</code> method.     * @param request servlet request     * @param response servlet response     */    protected void doGet(HttpServletRequest request, HttpServletResponse response)    throws ServletException, java.io.IOException {        processRequest(request, response);    }    /** Handles the HTTP <code>POST</code> method.     * @param request servlet request     * @param response servlet response     */    protected void doPost(HttpServletRequest request, HttpServletResponse response)    throws ServletException, java.io.IOException {        processRequest(request, response);    }    /** Simple servlet to demonstrate i18n capabilities of Application Server     */    public String getServletInfo() {        return "This is a simple servlet to demonstrate i18n capabilities of Application Server";    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -