码迷,mamicode.com
首页 > 编程语言 > 详细

Java EE之servlet处理表单提交的请求

时间:2016-12-24 19:41:53      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:occurs   proc   rri   script   理由   todo   orm   exception   oge   

1.在源包下新建一个Servlet页,取名为LoginServlet:

package weinidingServlet;                            //该Servlet所在的包

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;           //生成Servlet页面时自动导入的包
import java.sql.*;                  //需要自己添加的sql语句包
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import weinidingServlet.MyConnection;                     //需要自己添加的数据库连接和操作包
import weinidingJavaData.WndMembers;                  //需要自己添加的由数据库生成的实体类
/**
*
* @author 春霏
*/



@WebServlet(name = "LoginServlet", urlPatterns = {"/LoginServlet"})
public class LoginServlet extends HttpServlet {
public LoginServlet(){
super();
}


/**

* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)    //doPost函数主要处理由表单的post方法提交的数据,前提一定要是前端的form表单有一个method="post"
throws ServletException, IOException {
try {
response.setContentType("text/html;charset=UTF-8");
String name=request.getParameter("username");                             //request属性的getParameter方法获取前端表单提交的数据字段
String password=request.getParameter("password");
WndMembers member=new WndMembers();                                   //new一个由数据库生成的实体类
member.setWndMembersname(name);                                          //调用该实体类中的set方法,把get到得数据赋值给实体类
member.setWndMemberspw(password);
MyConnection my=new MyConnection();                                          //new一个数据库连接的类对象
my.init();                                                                                       //调用数据库连接对象的初始化方法
my.insert(member);                                                                      //执行插入方法


processRequest(request, response);                                               //用来返回相应后的前端页面
} catch (SQLException ex) {
Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet LoginServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet LoginServlet at "+request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}

 

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override

public String getServletInfo() {
return "Short description";
}// </editor-fold>

}

Java EE之servlet处理表单提交的请求

标签:occurs   proc   rri   script   理由   todo   orm   exception   oge   

原文地址:http://www.cnblogs.com/hqutcy/p/6217808.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!