一、当天完成的任务
开始eclipse配tomcat搭服务器,怎么个原理还是没搞懂,就先把代码码上去,大概是以自己电脑做服务器,将云端数据存到本地以便使用
服务器端的代码:Login
package com.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.ServletConfig; 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 implementation class LoginTest */ @WebServlet("/LoginTest") public class Login extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Login() { super(); // TODO Auto-generated constructor stub } public void init(ServletConfig config) throws ServletException { // TODO Auto-generated method stub super.init(config); try { Class.forName("com.mysql.jdbc.Driver"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); Connection con; Statement sql; String logname = request.getParameter("logname").trim(); String password = request.getParameter("password").trim(); String uri = "jdbc:mysql://localhost/MakeFriend"; try { con = DriverManager.getConnection(uri,"root","201513010223"); String condition = "select * from member where logname = '"+logname+"' and password = '"+password+"'"; sql = con.prepareStatement(condition); ResultSet rSet = sql.executeQuery(condition); if(rSet.next()) { out.println("login successfully!"); } else { out.println("can not login!"); } con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
Test:
package com.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.Statement; import javax.servlet.ServletConfig; 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 implementation class Test */ @WebServlet("/Test") public class Test extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Test() { super(); // TODO Auto-generated constructor stub } @Override public void init(ServletConfig config) throws ServletException { // TODO Auto-generated method stub super.init(config); try { Class.forName("com.mysql.jdbc.Driver"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); Connection con; Statement sql; //String logname = request.getParameter("logname").trim(); //String password = request.getParameter("password").trim(); String uri = "jdbc:mysql://localhost/MakeFriend"; } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
二、第二天的计划
继续搭服务器,J2ee没好好学啊
三、每日小结
①J2ee也太难了,当时只会了JDBC和jsp,servlet一点都没看啊
②MySQL的DOS怎么这么麻烦
③做好Dao和JDBC