笔记好几年前的,点比较老
一,服务端
1,配置文件web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 声明监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring-MVC映射访问路径
访问路径的配置,*.do,注意不要少写/或者*。 -->
<servlet>
<servlet-name>applicationContext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>applicationContext</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- Servlet映射访问路径 -->
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.server.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/Servlet/LoginServlet</url-pattern>
</servlet-mapping>
2,最原始的HttpServlet 形式
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* The doGet method of the servlet.
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username"); // 采用的编码是: iso-8859-1
String password = request.getParameter("password");
// 采用iso8859-1的编码对姓名进行逆转, 转换成字节数组, 再使用utf-8编码对数据进行转换, 字符串
// username = new String(username.getBytes("iso8859-1"), "gbk");
// password = new String(password.getBytes("iso8859-1"), "gbk");
username = new String(username.getBytes("iso8859-1"), "utf-8");
password = new String(password.getBytes("iso8859-1"), "utf-8");
System.out.println("姓名: " + username);
System.out.println("密码: " + password);
if("lisi".equals(username) && "123".equals(password)) {
/*
* getBytes 默认情况下, 使用的iso8859-1的编码, 但如果发现码表中没有当前字符,
* 会使用当前系统下的默认编码: GBK
*/
// response.getOutputStream().write("登录成功".getBytes("gbk"));
response.getOutputStream().write("登录成功".getBytes("utf-8"));
} else {
// response.getOutputStream().write("登录失败".getBytes("gbk"));
response.getOutputStream().write("登录失败".getBytes("utf-8"));
}
}
/**
* The doPost method of the servlet.
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doPost");
doGet(request, response);
}
}
3,Mvc方式
@Controller
public class TojsonByJDBC {
@RequestMapping("/search.do")
public void search(HttpServletRequest request,HttpServletResponse response) throws Exception{
String username = request.getParameter("username"); // 采用的编码是: iso-8859-1
String password = request.getParameter("password");
// 采用iso8859-1的编码对姓名进行逆转, 转换成字节数组, 再使用utf-8编码对数据进行转换, 字符串
// username = new String(username.getBytes("iso8859-1"), "gbk");
// password = new String(password.getBytes("iso8859-1"), "gbk");
username = new String(username.getBytes("iso8859-1"), "utf-8");
password = new String(password.getBytes("iso8859-1"), "utf-8");
System.out.println("姓名: " + username);
System.out.println("密码: " + password);
DataOutputStream outputStream=new DataOutputStream(response.getOutputStream());
outputStream.writeChars("success");
ArrayList<Person> pslist = (ArrayList<Person>) Search();
Gson gson = new Gson();
String json = gson.toJson(pslist);
json = "{\"data\":"+ json +"}";
// json = new String(json.getBytes("utf-8"));
// response.getOutputStream().write("登录成功".getBytes("utf-8"));
// outputStream.writeChars(json);
outputStream.write(json.getBytes("utf-8"));
// outputStream.writeUTF(json);
outputStream.close();
}
//1 插入数据
public boolean Add(Person person){
boolean flag=true;
Connection conn=null;
PreparedStatement ps=null; //创建PreparedStatement 对象
//sql语句不再采用拼接方式,应用占位符问号的方式写sql语句。
String sql= "insert into Person (Id,Name,Sex,Nation,Age,University,Specialities) values(?,?,?,?,?,?,?)";
try {
conn=DBConnUtil.getConn();
ps=conn.prepareStatement(sql);
ps.setString(1, person.getId()); //对占位符设置值,占位符顺序从1开始,第一个参数是占位符的位置,第二个参数是占位符的值。
ps.setString(2, person.getName());
ps.setString(3, person.getSex());
ps.setString(4, person.getNation());
ps.setString(5, person.getAge());
ps.setString(6, person.getUniversity());
ps.setString(7, person.getSpecialities());
int i=ps.executeUpdate();
if(i==0){
flag=false;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBConnUtil.closeAll(null, ps, conn);
}
return flag;
}
//2 修改数据
public boolean Update(Person person){
boolean flag=true;
Connection conn=null;
PreparedStatement ps=null;
String sql="update Person set Age=? where Name=?";
try {
conn=DBConnUtil.getConn();
ps=conn.prepareStatement(sql);
ps.setString(1, person.getAge());
ps.setString(2, person.getName());
int i= ps.executeUpdate();
if(i==0){
flag=false;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBConnUtil.closeAll(null, ps, conn);
}
return flag;
}
//3 删除数据
public boolean Delete(String Name){
boolean flag=true;
Connection conn=null;
PreparedStatement ps=null;
String sql="delete from Person where Name=?";
try {
conn=DBConnUtil.getConn();
ps=conn.prepareStatement(sql);
ps.setString(1, Name);
int i=ps.executeUpdate();
if(i==0){
flag=false;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBConnUtil.closeAll(null, ps, conn);
}
return flag;
}
//4 查看数据
public List<Person> Search(){
List<Person> list= new ArrayList<Person>();
Connection conn = null;
PreparedStatement ps = null;//创建Statement
ResultSet rs = null;//ResultSet类似Cursor
String sql="select * from Person";
try {
conn=DBConnUtil.getConn();
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
Person person= new Person();
person.setId(rs.getString("Id"));
person.setName(rs.getString("Name"));
person.setAge(rs.getString("Age"));
person.setSex(rs.getString("Sex"));
person.setNation(rs.getString("Nation"));
person.setUniversity(rs.getString("University"));
person.setSpecialities(rs.getString("Specialities"));
list.add(person);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBConnUtil.closeAll(rs, ps, conn);
}
return list;
}
//5,查询(动态SQL语句)
private ArrayList<SanGuoHeroBean> Search1(String strCountry, String strSex, String strOperatorDateS,
String strOperatorDateE, String pageNO, String pageSize) {
ArrayList<SanGuoHeroBean> list= new ArrayList<SanGuoHeroBean>();
Connection conn = null;
PreparedStatement ps = null;//创建Statement
ResultSet rs = null;//ResultSet类似Cursor
//拼接Sql语句
String sql = "SELECT * FROM SanGuoHero where 1 = 1";
List<String> params = new ArrayList<String>();
if(!StringUtil.isEmpty(strCountry)){
sql = sql + " and University = ?";
params.add(strCountry);
}
if(!StringUtil.isEmpty(strSex)){
sql = sql + " and Sex = ?";
params.add(strSex);
}
if(!StringUtil.isEmpty(strOperatorDateS)&&!StringUtil.isEmpty(strOperatorDateE)){
sql = sql + " and CreatDate between ? and ?";
params.add(strOperatorDateS);
params.add(strOperatorDateE);
}
try {
conn=DBConnUtil.getConn();
ps=conn.prepareStatement(sql);
//设置不定长参数
for(int i=0;i<params.size();i++){
ps.setString(i+1, params.get(i));
}
rs=ps.executeQuery();
while(rs.next()){
SanGuoHeroBean hero= new SanGuoHeroBean();
hero.setId(rs.getString("id"));
hero.setName(rs.getString("Name"));
hero.setAge(rs.getString("Age"));
hero.setSex(rs.getString("Sex"));
hero.setNation(rs.getString("Nation"));
hero.setUniversity(rs.getString("University"));
hero.setSpecialities(rs.getString("Specialities"));
hero.setArms(rs.getString("Arms"));
hero.setMount(rs.getString("Mount"));
hero.setCreatDate(rs.getString("CreatDate"));
hero.setUpdateDate(rs.getString("UpdateDate"));
list.add(hero);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBConnUtil.closeAll(rs, ps, conn);
}
return list;
}
}
二,Android端
HttpPost等已经过时,只是查看路径
//使用Servlet处理客户端请求
// HttpPost post = new HttpPost("http://192.168.1.121:9021/Logindata/Servlet/LoginServlet");
//使用Spring-MVC处理客户端请求
// HttpPost post = new HttpPost("http://192.168.1.121:9021/Server-Android/fromparams.do");
HttpPost post = new HttpPost("http://192.168.1.121:9021/Server-Android/fromparams.do?username=kkk&password=123");