📄 ocrservlet.java
字号:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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 com.db.Connectionmysql;
import com.mysql.jdbc.PreparedStatement;
import com.page.Show;
public class OCRServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public OCRServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* 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 {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* 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 {
System.out.println("开始了");
// 设置编码格式把浏览器送来的格式现转换为GB2312 同时把返回浏览器的数据格式设置为text/html;charset=GB2312
request.setCharacterEncoding("GB2312");
response.setContentType("text/html;charset=GB2312");
PrintWriter out = response.getWriter();
Show show = new Show();
Connection con=null;
java.sql.PreparedStatement stmt=null;
/*
* Connection con=null; java.sql.PreparedStatement stmt=null; ResultSet
* rs=null;
*/
// 获取action的值 知道是进行那项操作
String action = request.getParameter("action");
System.out.println(action);
// 产生验证码 并返回客户端
if (action.equals("login")) {
// 得到手机号
String tel = request.getParameter("tel");
String password = request.getParameter("password");
// 下面调用手机发送验证码到后台的代码,返回boolean值,如果为true即交费成功的话就把验证码存入数据库,用户可以根据次验证码登陆,进行相应的操作
System.out.println(tel);
System.out.println(password);
HttpSession session = request.getSession(true);
String rand = (String) session.getAttribute("rand");
System.out.println(rand);
// 如果收费成功返回true if成立,在执行密码判断 在true中放收费成功与否的方法
//这儿假设收费成功
//一下代码就直接将消费记录存入数据库,如果要和手机平台结合的话,可以调用com.consume.Consume类的方法用于交费判断和存数据库
if (true) {
//付费成功把消费存入数据库
con=Connectionmysql.getconnection();
String sql="insert into consume (tel,money) values ('"+tel+"',5.00)";
try {
stmt=con.prepareStatement(sql);
int i=stmt.executeUpdate();
if(i==1){
System.out.println("数据存入成功");
System.out.println("数据存入成功");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 密码符合则跳转到识别页面
if (password.equals(rand)) {
//把手机号和交费记录存入数据库
//首先从短信平台中获取手机号 和交纳的费用用于存入数据库 这里手机号从页面获取 价格设为一次5元
/*ServletContext sc = getServletContext();
RequestDispatcher rd = sc
.getRequestDispatcher("../uploadfile.jsp");
rd.forward(request, response);*/
response.sendRedirect("../uploadfile.jsp");
} else {
out.print(show.errorBox("密码输入错误", "提示"));
}
}
else{
out.print(show.errorBox("收费还没有成功,请稍后在登陆", "提示"));
}
// 获取connection连接
// con=Connectionmysql.getconnection();
// String sql="insert into user (tel,password ) values ('"+tel+"'
// ,'"+rand+"')";
/*
* try { stmt=con.prepareStatement(sql); int i=stmt.executeUpdate();
* if(i==0){ out.print(show.errorBox("shibai", "tishi")); }
* } catch (SQLException e) { // TODO Auto-generated catch block
* e.printStackTrace(); }
*/
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -