mysql jsp 登陆界面出错

Daisyiko 2018-02-24 05:18:43

...全文
1230 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Daisyiko 2018-03-15
  • 打赏
  • 举报
回复
解决了就是web,xml里面没有配置好
weixin_39909641 2018-03-08
  • 打赏
  • 举报
回复
@WebServlet("/需要的路径") 或者web.xml里面配。
Miracle1216 2018-03-08
  • 打赏
  • 举报
回复
servlet有没有在web.xml里面配置啊
叫我XiaoNi 2018-03-06
  • 打赏
  • 举报
回复
form中action要和web.xml中<url-partten>配置一致
脾气太臭 2018-03-01
  • 打赏
  • 举报
回复
如果你配置文件没有问题的话 找不到对应文件导致404
  • 打赏
  • 举报
回复
form表单中的提交地址看看对不对,或者xml文件里面配一下。404就是找不到文件,就是地址的问题
亲爱的Joe 2018-02-25
  • 打赏
  • 举报
回复
web.xml贴下,还有其他配置文件贴下。 可能是你没配置data/LoginServelet
weixin_39670158 2018-02-24
  • 打赏
  • 举报
回复
@WebServlet(自己的路径) 或者web.xml里面配。
hi,你礼貌吗 2018-02-24
  • 打赏
  • 举报
回复
@service
Daisyiko 2018-02-24
  • 打赏
  • 举报
回复
package data;

import java.io.IOException;
import java.sql.ResultSet;

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 {
	public void doPost(HttpServletRequest request,
			           HttpServletResponse response)
			throws ServletException, IOException {
		String toJSP="";
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		String sql="select * from user where username='"+username+"'" +
				   "&&password='"+password+"'";
		JdbcBean jdbc=new JdbcBean();
		try{
			ResultSet rs=jdbc.query(sql);
			if(rs.next()){
				 request.getRequestDispatcher("success.jsp").forward(request, response);  
			}
			else{
				  request.getRequestDispatcher("error.jsp").forward(request, response);  
			}
			rs.close();
		}
		catch(Exception e){
			System.out.println("发生查询异常");
		}
		RequestDispatcher rd=request.getRequestDispatcher(toJSP);
		rd.forward(request, response);
	} 
	public void doGet(HttpServletRequest request, 
			          HttpServletResponse response)
	  throws ServletException, IOException {
            doPost(request,response);
    }
}
Daisyiko 2018-02-24
  • 打赏
  • 举报
回复
package data;
import java.sql.*;
public class JdbcBean {
	  private String DBDriver="com.mysql.jdbc.Driver";
	  private String DBUrl="jdbc:mysql://localhost:3306/test";
	  private String DBUser="localhost@root";
	  private String DBPassword="980514";
	  public Connection conn=null;
	  public ResultSet rs=null;
	  public ResultSet query(String sql)throws Exception{
		  try{
			  Class.forName(DBDriver);
			  conn=DriverManager.getConnection(DBUrl,DBUser,DBPassword);
			  Statement stmt=conn.createStatement();
			  rs=stmt.executeQuery(sql);
			  return rs;
		  }
		  catch(SQLException e){
			  System.out.println(e.getMessage());
		  }
		  return null;
	  }
	  public void update(String sql)throws Exception{
		  try{
			  Class.forName(DBDriver);
			  conn=DriverManager.getConnection(DBUrl,DBUser,DBPassword);
			  Statement stmt=conn.createStatement();
			  stmt.executeUpdate(sql);
			  stmt.close();
			  conn.close();
		  }
		  catch(SQLException e){
			  System.out.println(e.getMessage());
		  }
	  }
}
Daisyiko 2018-02-24
  • 打赏
  • 举报
回复
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>  
    <title>My JSP 'login.jsp' starting page</title>
    <script>
            function myFunction(){
               if(myForm.username.value==""){
                  alert("用户名不能为空");
               }
               else if(myForm.password.value==""){
                  alert("密码不能为空");
               }
               else{
                  myForm.submit();
               }
            }
    </script>
  </head>
  <body>
  <form action="data/LoginServlet" method="post" name="myForm"> 
  <center>
  <table>
     <tr>
        <td>用户名</td>
        <td><input type="text" name="username"></td>
        <td>用户名不能为空</td>
     </tr>
     <tr>
        <td>密码</td>
        <td><input type="password" name="password"></td>
        <td>密码不能为空</td>
     </tr>
     <tr>
        <td></td>
        <td><input type="submit" value="提交" onclick="myFunction()">
            <input type="reset" value="取消"></td>
     </tr>
  </table>
  </center>
  </form>
  </body>
</html>

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧