ajax+servlet实现省份城市动态效果 demo

【JSP】

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'one.jsp' starting page
    
	
	
	    
	
	
	
	

  
  
  
     
 

  


【servlet】

package com.wh;

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;

public class SelectCityServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */ 
	public SelectCityServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String state = request.getParameter("state"); StringBuffer sb = new StringBuffer(""); if("zj".equals(state)){ sb.append("hangzhouhuzhou"); }else if("zs".equals(state)){ sb.append("nanjingsuzhouyangzhou"); } sb.append(""); PrintWriter out =response.getWriter(); out.write(sb.toString()); out.close(); } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
【WEB.XML】



  
    This is the description of my J2EE component
    This is the display name of my J2EE component
    SelectCityServlet
    com.wh.SelectCityServlet
  

  
    SelectCityServlet
    /SelectCityServlet
  
  
    index.jsp
  


这种方式 比较简单,就酱紫==

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


oh ,god!

忘了,上面是用的字母

呃……

换成中文出现乱码的话就需要在servlet里面设置下相应的编码格式,即:

response.setCharacterEncoding("UTF-8");和JSP中的编码格式一致

从servlet传给JSP页面的中文显示就不会出现乱码了

QQQ


你可能感兴趣的:(ajax+servlet实现省份城市动态效果 demo)