Pictures of Ajax user with the user name change

<Javascript> code as follows:

<Script type = "text / JavaScript"> 
function MyCheck () { 
     // Get the value of the user name text box var the uname
= document.getElementById ( "the uname" ) .Value; the try { XMLHTTP = new new ActiveObject ( "Microsoft.XMLHTTP " ); } the catch (E) { XMLHTTP = new new the XMLHttpRequest (); } // set the connection, the value of the user in the text box to pass to the servlet xmlhttp.open (" post "," ../LoginPathServlet?uname= "+ the uname); // setting request header xmlhttp.setRequestHeader (" the Content-type " , "file application / X-WWW-form-urlencoded" ); // set the callback function xmlhttp.onreadystatechange = function () { IF (xmlHttp.readyState ==. 4 ) { IF (== 200 is xmlhttp.status ) {
            // Get pass back value var S
= xmlhttp.responseText;
            // set the picture image path to the path of the user image document.getElementById (
"TX") the src =. S; } } } xmlhttp.send ( null ); } </ Script>

<Body> code as follows:

<div class="login_title">
          <img id="tx" src="默认图片路径 width="50px" height="50px">
 </div>
<div class="item item-fore1">
   <input name="uname" onkeyup="mycheck();" type="text" class="text"
    id="uname" placeholder="请输入用户">
 </div>

Servlet code as follows:

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import entity.Uuser1;
import biz.imp_biz.Imp_Uuserbiz;

public class LoginPathServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        // 设置编码
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        //接收数据
        Request.getParameter the uname = String ( "the uname" ); 
        
                // connect to the database determines whether the user name exists 
        Imp_Uuserbiz IU = new new Imp_Uuserbiz (); 
        Uuser1 U =     iu.selectbyname (the uname);
                  // set a default image path 
        String str = " ../upload/hehe.jpg " ;
                // If the user exists, the user image transmission path back 
        IF (= U! null ) {
             IF (u.getPhoto () =! null {) 
                STR =" ../ "+ u.getPhoto (); 
            } 
        } 
        Out.print (STR); 
        
    } 

}    

After the realization of functions:

 

 

 

Guess you like

Origin www.cnblogs.com/kingpp/p/10931448.html