菜鸡求救,要被弄哭了

最近学校要作业个java web大作业,但自己跟着往上教程写写了很久都没办法正常跑起来,找了很多方法都不行,不知道是什么问题
希望有大佬帮帮忙康康吧,谢谢了

平台是Tomcat8+MSSQL 2012+MyEclipse2012+JDK1.8
数据库连接在MyEclipse里的DB Browser里新建了一个连接,测试连接也是成功的

做的是JSP+JavaBean+Servlet,作业个登录功能
是跟着这个做的 https://blog.csdn.net/renascence_mcc/article/details/84729003

报错信息:

java.lang.NullPointerException
	com.CameraStore.servlet.MainServlet.doGet(MainServlet.java:32)
	com.CameraStore.servlet.MainServlet.doPost(MainServlet.java:66)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

MainServlet

package com.CameraStore.servlet;


import  java.sql.*;
import  java.io.*;
import  javax.servlet.*;
import  javax.servlet.http.*;
import  com.CameraStrore.connector.*;
import  com.CameraStore.dao.*;

public class MainServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6076122595891851840L;
	public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,
    IOException{
			request.setCharacterEncoding("gb2312");
			String  usr=request.getParameter("username");
			String  pwd=request.getParameter("password");
			boolean validated=false;
			SqlSrvDBConn sqlsrvdb=new SqlSrvDBConn();
			HttpSession session=request.getSession();
			User user=null;
			user=(User)session.getAttribute("user");
				if(user==null){
					String sql="select*from User";
					ResultSet rs=sqlsrvdb.executeQuery(sql);
					try{
		
	
						while(rs.next())
						{
							if((rs.getString("username").trim().compareTo(usr)==0)
									&&(rs.getString("password").compareTo(pwd)==0))
							{
								user=new User();
								user.setId(rs.getInt(1));
								user.setUsername(rs.getString(2));
								user.setPassword(rs.getString(3));
								session.setAttribute("user",user);  //把user对象存储在会话中
								validated=true;  //标识为true表示验证成功通过
							}
						}
						rs.close();
					}catch(SQLException e){
						e.printStackTrace();
					}
	sqlsrvdb.closeStmt();
	sqlsrvdb.closeConn();
}
else{
	validated=true;
}
if(validated)
{
	response.sendRedirect("main.jsp");
}
else{
	response.sendRedirect("LoginFail.jsp");
	
}
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,
       IOException{
    	doGet(request,response);
    	
    }




}


数据库连接:

package com.CameraStrore.connector;

import java.sql.Connection;
import java.sql.*;

public class SqlSrvDBConn {

	private Statement stmt;
	private Connection conn;
	ResultSet rs;
	public SqlSrvDBConn(){
		stmt=null;
		try{
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			
			conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName=CameraProject","test1","123456");
			
		}catch(Exception e){
			e.printStackTrace();
		}
		rs=null;
	}
	//执行查询类语句,又返回集
	public ResultSet executeQuery(String sql)
	{
		try{
			stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					                                          ResultSet.CONCUR_UPDATABLE);
				rs=stmt.executeQuery(sql);
			}catch(SQLException e){
				System.err.println("Data.executeQuery:"+e.getMessage());
			}
		return rs;
	}
	//关闭对象
	public void closeStmt()
	{
		try{
			stmt.close();    //关闭Statement对象
		}catch(SQLException e){
			System.err.println("Data.executeQuery:"+e.getMessage());
		}
	}
	public void closeConn()
	{
		try{
			conn.close();     //关闭连接
		}catch(SQLException e){
			System.err.println("Data.executeQuery:"+e.getMessage());
		}
	}
}


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <welcome-file-list>
    	<welcome-file>Welcome.jsp</welcome-file>
    	<welcome-file>index.htm</welcome-file>
   	 	<welcome-file>login.jsp</welcome-file>
    	<welcome-file>default.html</welcome-file>
    	<welcome-file>default.htm</welcome-file>
    	<welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
   <servlet>
  		<servlet-name>mainServlet</servlet-name>
  		<servlet-class>com.CameraStore.servlet.MainServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  		<servlet-name>mainServlet</servlet-name>
 		<url-pattern>/mainServlet</url-pattern>
  </servlet-mapping>
  
    <servlet>
  		<servlet-name>selectAll</servlet-name>
  		<servlet-class>com.CameraStore.servlet.SelectAllServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  		<servlet-name>selectAll</servlet-name>
 		<url-pattern>/selectAll</url-pattern>
  </servlet-mapping>
</web-app>

恳请大佬帮忙看看,万分感激

  • 0
    点赞
  • 2
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:游动-白 设计师:我叫白小胖 返回首页

打赏作者

JasonWithMiracle

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值