Home > Back-end >  Too weird, questions about the ServletContext access online response, etc
Too weird, questions about the ServletContext access online response, etc

Time:03-05

A few days ago on the Internet to download a chat client based on the advice of the Servlet (Java) look at the source code, and try to run, but how also couldn't find a chat about the visitor list, and the message list to ServletContext variable setAttribute () code, but the demonstration process and the code behind the visitor list and the message list from ServletContext. Indeed gettribute (" messagelist ") ServletContext. Gettribute (" userlist) "I just don't understand, no program code to ServletContext call setAttribute () to store data, but it is how to get the userlist and messagelist? The code below everybody to check
Package session_test;

import java.io.IOException;
Import the Java. IO. PrintWriter;
Import the Java. Util. Vector;
The import javax.mail. Servlet. ServletContext;
The import javax.mail. Servlet. ServletException;
The import javax.mail. Servlet. HTTP. HttpServlet;
The import javax.mail. Servlet. HTTP. It;
The import javax.mail. Servlet. HTTP. HttpServletResponse;
/*
* sends a message */
Public class MessageServlet extends the HttpServlet {
Public void doGet (it request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html; GBK charset=");
PrintWriter out=response.getWriter();
Request. SetCharacterEncoding (" GBK ");
Out.println (" & lt; Html>" );
Out.println (" & lt; head> MessageServlet " );
Out.println (" & lt; Body>" );
Request. SetCharacterEncoding (" GBK ");
ServletContext context=getServletContext ();
//retrieve information forms
String usr=request. The getParameter (" uname ");
System. The out. Println (" MessageServlet: user "+ usr);
String STR=request. The getParameter (" message ");
The Vector HSH=(Vector) context. The getAttribute (" messagelist ");
If (STR!=null) {
HSH. Add (usr + ":" + STR);
}
Out.println (" & lt; The form method="post 'action=' MessageServlet? Uname="+ usr + '& gt;" " );
System. The out. Println (" MessageServlet2: user: "+ usr);
Out.println (" & lt; Hr>" );
Out.println (" input message: & lt; Input type='test' name='message'/& gt;" );
Out.println (" & lt; Input type="submit" value='https://bbs.csdn.net/topics/'/& gt;" );
Out.println (" & lt;/form>" );
Out.println (" & lt; The iframe frameboder=0 height=546 marginheight=0 marginwidth=0 "
+ "scrolling=auto SRC=" https://bbs.csdn.net/topics/DisplayServlet "width='100% height=' 80% '");
Out.println (" & lt;/iframe>" );
Out.println (" & lt;/body>" );
Out.println (" & lt;/html>" );
out.close();
}
Public void doPost (it request, HttpServletResponse response) throws ServletException, IOException {
DoGet (request, response);
}
}

import java.io.IOException;
Import the Java. IO. PrintWriter;
Import the Java. Util. Vector;
Import the Java. Util. Logging. The Logger;
The import javax.mail. Enterprise. Context. Spi. The context;
The import javax.mail. Servlet. ServletContext;
The import javax.mail. Servlet. ServletException;
The import javax.mail. Servlet. HTTP. HttpServlet;
The import javax.mail. Servlet. HTTP. It;
The import javax.mail. Servlet. HTTP. HttpServletResponse;
/*
* according to the user and chat messages
*/
Public class DisplayServlet extends the HttpServlet {
Public void doGet (it request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html; GBK charset=");
PrintWriter out=response.getWriter();
ServletContext context=getServletContext ();
Request. SetCharacterEncoding (" GBK ");
Out.println (" & lt; Html>" );
Out.println (" & lt; Head>" );
Out.println (" & lt; Meta HTTP - equiv='refresh' content='3'/& gt;" );
Out.println (" & lt; Tilte> MessageServlet " );
Out.println (" & lt; Body>" );
Request. SetCharacterEncoding (" GBK ");
Out.println (" content ");
//show chat information
The Vector hab=(Vector) context. The getAttribute (" messagelist ");
Out.println (" & lt; The textarea name='info' rows='30' & gt;" );
for(int i=0; iOut.println (hab) get (I));
}
Out.println (" & lt;/textarea>" );
Out.println (" user list ");
Out.println (" & lt; The textarea name='user' rows='30' & gt;" );
//show the user list
The Vector vc=(Vector) context. The getAttribute (" userlist ");
for(int i=0; iIf (vc. Get (I)!=null)
Out.println (vc) get (I));
}
Out.println (" & lt;/textarea>" );
Out.println (" & lt;/body ");
Out.println (" & lt;/html>" );
out.close();
}
Public void doPost (it request, HttpServletResponse response) throws ServletException, IOException {
DoGet (request, response);
}
}

Public class MainServlet extends the HttpServlet {
Private static String CONTENT_TYPE="text/HTML. GBK charset=";
//add user request dispatching
Public void doGet (it request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html; GBK charset=");
//response. SetContentType (CONTENT_TYPE);
PrintWriter out=response.getWriter();
Request. SetCharacterEncoding (" GBK ");

Out.println (" & lt; Html>" );
Out.println (" & lt; head> MainServlet " );
Out.println (" & lt; Body>" );
Out.println (" & lt; H3 & gt; Enter the chat system... nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull