Two Ajax methods (Summary) and two ajax Methods

Source: Internet
Author: User

Two Ajax methods (Summary) and two ajax Methods

The Controller Servlet provides a simple change:

For Ajax systems, the server response does not need to be the content of the entire page, but can only be

Required data. The controller cannot forward data requests to the jsp page.

The Controller has two options:

1. directly generate simple response data.

In this mode, the Servlet directly obtains the page output stream through response

The output stream generates a character response.

Package pers. zkr. chat. web; 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; import pers. zkr. chat. service. chatService; @ WebServlet (urlPatterns = {"/chat. do "}) public class ChatServlet extends HttpServlet {@ Override public void service (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub request. setCharacterEncoding ("UTF-8"); String msg = request. getParameter ("chatMsg"); System. out. println (msg); if (msg! = Null &&! Msg. equals ("") {String user = (String) request. getSession (). getAttribute ("user"); System. out. println (user + "user"); ChatService. instance (). addMsg (user, msg);} // set the response content type <strong> response. setContentType ("text/html; charset = UTF-8"); // gets the page output stream PrintWriter out = response. getWriter (); // directly generate the response out. println (ChatService. instance (). getMsg (); </strong> request. setAttribute ("msg", ChatService. instance (). getMsg (); forward ("/chat. jsp ", request, response);} private void forward (String url, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub request. getRequestDispatcher (url ). forward (request, response );}}

2. Turn to a simple jsp and use the JSP page to generate a simple response.

The Controller forwards requests to another JSP page, while the JSP page only outputs chat information.

Here, a jsp page is required to receive data from the Controller, which is also the response text of the server.

The original page only needs:

1) Create an XMLHttpRequest object

2) send a request

3) receive Server Response

Package org. crazyit. chat. web; import javax. servlet. *; import javax. servlet. http. *; import javax. servlet. annotation. *; import java. io. *; import org. crazyit. chat. service. *;/*** Description: * <br/> website: <a href = http://www.bkjia.com > Helper house </a> * <br/> Copyright (C), 2001-2014, Yeeku. h. lee * <br/> This program is protected by copyright laws. * <br/> Program Name: * <br/> Date: * @ version 1.0 */@ WebServlet (urlPatterns = {"/chat. do "}) public class ChatServlet extends HttpServlet {public void service (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {// set to use the GBK character set to parse request parameters. setCharacterEnco Ding ("UTF-8"); String msg = request. getParameter ("chatMsg"); if (msg! = Null &&! Msg. equals ("") {// get the current user String user = (String) request. getSession (true ). getAttribute ("user"); // call addMsg of ChatService to add ChatService. instance (). addMsg (user, msg);} // set all chat information to the request attribute <strong> request. setAttribute ("chatList", ChatService. instance (). getMsg (); </strong> // forward it to chatreply. jsp page forward ("/chatreply. jsp ", request, response);} // Method for executing the forwarding request private void forward (String url, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// execute the forwarding request. getRequestDispatcher (url ). forward (request, response );}}

Page for receiving data

<% @ Page contentType = "text/html; charset = GBK" errorPage = "error. jsp "%> <% -- output current chat information -- % >$ {requestScope. chatList}

Html page

<! DOCTYPE html> 

The above two Ajax solutions to redundant refresh methods (Summary) are all the content shared by Alibaba Cloud. I hope you can give us a reference and support for the customer's house.

Related Article

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.