index.jsp
<%@ page language= "java" pageencoding= "UTF-8"%>
Selectservlet.java
Package com;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse; /*** * * * @author ZDW * */public class Selectservlet extends HttpServlet {private static final long Serialversi
Onuid = 1L;
Public Selectservlet () {super ();
public void Destroy () {Super.destroy (); public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IO
Exception {//Response.setcharacterencoding ("GBK");
Response.setcontenttype ("Text/xml");
Response.setheader ("Cache-control", "No-cache");
Request.setcharacterencoding ("GBK");
Response.setcharacterencoding ("UTF-8");
String Targetid = request.getparameter ("id"). toString ();
System.out.println (Targetid); Gets the value of the parameter ID in the request String Xml_start= "<selects>";
String xml_end = "</selects>";
String xml = ""; if (Targetid.equalsignorecase ("0")) {XML = <select><value>0</value><text> Please select
</text></select> "; else if (Targetid.equalsignorecase ("1")) {xml = "<select><value>1</value><text
> Changping </text></select> ";
XML = "<select><value>2</value><text> Fengtai </text></select>";
XML + = "<select><value>3</value><text> Haidian </text></select>";
XML = "<select><value>4</value><text> Chaoyang </text></select>"; else if (Targetid.equalsignorecase ("2")) {xml = "<select><value>1</value><text
> Tanggu District </text></select> "; XML = "<select><value>2</value><text> Hangu District </text>≪/select> ";
XML + = "<select><value>3</value><text> Grand Port District </text></select>";
XML + = "<select><value>4</value><text> Dongli District </text></select>"; else {//If 3, returns the following character XML = "<select><value>1</value><text> Jinan </text>&
Lt;/select> ";
XML = "<select><value>2</value><text> qingdao </text></select>";
XML = "<select><value>3</value><text> Zibo </text></select>";
XML = "<select><value>4</value><text> Zaozhuang </text></select>";
String Last_xml = xml_start + xml + xml_end;
Response.getwriter (). write (Last_xml); public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, I
oexception {doget (request, response);
}public void Init () throws servletexception {}}
Xml
<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "
2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http://java.sun.com/xml/ns/ Java
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
<servlet>
<servlet-name> Selectservlet</servlet-name>
<servlet-class>com. selectservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> selectservlet</servlet-name>
<url-pattern>/select</url-pattern>
</ servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>