小白,学习记录一下憋了一个晚上的问题,最后都记录下吧
解决方法在文章最后
目录
遇见的问题截图
以为是这个查了不是
后来卡在这里了,一直以为是不是我代码有问题,或者数据库没连接上,是不是连接数据库代码有问题,然后反复纠结,网上查,什么buildpath mysql-connector-java-3.0.11-stable-bin.jar包没导好,也按照网上建立数据库也不行。
最后发现确实是没链接上(笑哭)jar包放错位置了......
部分代码,最后是没问题的
register.html
<!DOCTYPE html>
<html>
<head>
<title>register.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
register
<form name="f1" id="f1" action="servlet/LoginServlet" method="post">
<table>
<tr>
<td>name:</td>
<td><input type="text" name="username" id="login"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="userpassword" id="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="register"></td>
</tr>
</table>
</form>
</body>
</html>
LoginServlet.java
package userinfo;
import java.io.IOException;
import java.io.PrintWriter;
//import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
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 {
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>");
//RequestDispatcher dis=request.getRequestDispatcher("../handleLogin.jsp");
//RequestDispatcher dis=request.getRequestDispatcher("http://www.dlou.edu.cn");
//dis.forward(request, response);
String name=request.getParameter("username");
String password=request.getParameter("userpassword");
FirstJavaBean fj=new FirstJavaBean();
//boolean result=fj.verifyLogin(name, password);
boolean result=fj.register(name, password);
if(result)
out.println(" successful!");
else
out.println("failure!");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
解决方法
那个jar包我是放到 tomcat/lib 下了
千万别跟着项目,然后放到 WEB-INF/lib 下*****************
最后windows那个图片的
然后右键你的库我的是Connect
Add JARs把你刚才在tomcat/lib下的jar包导进来,把原来的remove掉就好了。
舒服了~
最后在解释下那个为啥不能放WEB-INF/lib下,因为在myeclipse中,如果把jsp啥的写在WEB-INF下的话,将受到访问保护,需要配置web.xml来访问。