javaweb实现登录案例

功能:给出登录页面,登陆成功。给出成绩页面并且支持修改成绩分数。
效果展示
javaweb实现登录案例_第1张图片
javaweb实现登录案例_第2张图片
总结:麻雀虽小,五脏俱全。登陆成功的页面支持修改成绩,实时反馈,登录页面涉及了jqueryUI设计警告弹窗+ajax向服务器传送数据。剩下的就是和数据库的操作。

login.jsp(页面)
利用jqueryUI设计弹出警告框。通过ajax向服务器发送请求,并返回结果。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>



	
	
	Document
	
    
    
    
	




	

Login

success.jsp(成绩页面)

<%@page import="com.item.domain.Studentscore"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here



<% 
	ArrayListarraylist=(ArrayList)request.getAttribute("arraylist");
%>
	
	<% for(Studentscore stu:arraylist){%>
	
		<% }%>
		
	
姓名 课程 成绩
<%=stu.getName() %> <%=stu.getCourse()%> <%=stu.getScore() %>

修改成绩:

姓名:
课程:
成绩:
              

com.item.servlet.demo(Servlet)
LoginServlet1.java

package com.item.servlet.demo;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.item.domain.Student;
import com.item.domain.Studentscore;
import com.item.model.UesrModel;
import com.item.utiles.SearchStudent;

/**
 * Servlet implementation class LoginServlet
 * 
 */
@WebServlet(urlPatterns = "/LoginServlet1",
initParams = {
		@WebInitParam(name="times",value="30")
}
)
public class LoginServlet1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
	int sum=10;
	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
		System.out.println("Servlet被初始化");
		ServletConfig servletConfig = this.getServletConfig();
		String initParameter = servletConfig.getInitParameter("times");
		sum=Integer.parseInt(initParameter);
	}

	/**
	 * 
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		if(sum<=0) {
			response.getWriter().print("登录次数已达上限,禁止使用!");
			return ;
		}
		//response.getWriter().print("访问"+sum+"次");
		sum--;
		String loginStatus=request.getParameter("loginStatus");
		if(loginStatus.equals("loginsuccess")) {
			try {
				LoginSuccess(request,response);
			} catch (ServletException | IOException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return ;
		}
		String sno=request.getParameter("sno");
		String psw=request.getParameter("psw");
		//System.out.println(sno+" ,"+psw);
		Student student=new Student();
		student.setSno(sno);
		student.setPwd(psw);
		UesrModel usermodel=new UesrModel();
		try {
			Student login = usermodel.login(student);
			if(login==null) {
				response.getWriter().print("error");
//				 response.sendRedirect("login.jsp");
				return ;
			}else {
				response.getWriter().print("loginsuccess");
				return ;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	private void LoginSuccess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		// TODO Auto-generated method stub
		SearchStudent searchStudent=new SearchStudent();
		ArrayList<Studentscore>arrayList=searchStudent.Searchcourse();
		request.setAttribute("arraylist", arrayList);
		request.getRequestDispatcher("success.jsp").forward(request, response);
	}

	/**
	 * @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);
	}

}

UpdateServlet.java

package com.item.servlet.demo;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.item.domain.Studentscore;
import com.item.utiles.SearchStudent;
import com.item.utiles.UpdateStudent;

/**
 * Servlet implementation class UpdateServlet
 */
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name=request.getParameter("name");
		String course=request.getParameter("course");
		String score=request.getParameter("score");
		int scores;
		scores=Integer.parseInt(score);
		Studentscore stu=new Studentscore();
		stu.setName(name);
		stu.setCourse(course);
		stu.setScore(scores);
		UpdateStudent upStudent=new UpdateStudent();
		try {
			upStudent.Update(stu);
			SearchStudent searchStudent=new SearchStudent();
			ArrayList<Studentscore>arrayList=searchStudent.Searchcourse();
			request.setAttribute("arraylist", arrayList);
			request.getRequestDispatcher("success.jsp").forward(request, response);
		} 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);
	}

}

com.item.utiles(一些工具包)
DButiles.java

package com.item.utiles;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DButiles {
	private static ComboPooledDataSource dataSource=new ComboPooledDataSource();
	public static DataSource getDADataSource() {
		return dataSource;
	}
}

SearchStudent.java

package com.item.utiles;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.item.domain.Studentscore;



public class SearchStudent {
	public ArrayList<Studentscore> Searchcourse() throws SQLException {
		QueryRunner queryRunner=new QueryRunner(DButiles.getDADataSource());
		String sql="select * from score";
		ArrayList<Studentscore>arraylist=(ArrayList<Studentscore>) queryRunner.query(sql,new BeanListHandler<Studentscore>(Studentscore.class));
//		for(Studentscore stu:arraylist) {
//			System.out.println(stu.getName()+" "+stu.getCourse()+" "+stu.getScore());
//		}
		return arraylist;
	}
	public static void main(String[] args) throws SQLException {
		QueryRunner queryRunner=new QueryRunner(DButiles.getDADataSource());
		String sql="select * from score";
		ArrayList<Studentscore>arraylist=(ArrayList<Studentscore>) queryRunner.query(sql,new BeanListHandler<Studentscore>(Studentscore.class));
		for(Studentscore stu:arraylist) {
			System.out.println(stu.getName()+" "+stu.getCourse()+" "+stu.getScore());
		}
	}
	
}

UpdateStudent.java

package com.item.utiles;

import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.item.domain.Studentscore;

public class UpdateStudent {
	public void Update(Studentscore stu) throws SQLException {
		QueryRunner queryRunner=new QueryRunner(DButiles.getDADataSource());
		String sql="update score set score=? where course=?";
		int cnt=queryRunner.update(sql,stu.getScore(),stu.getCourse());
		if(cnt>0) {
			System.out.println("修改成功");
		}else {
			System.out.println("修改失败");
		}
	}
//	public static void main(String[] args) throws SQLException {
//		QueryRunner queryRunner=new QueryRunner(DButiles.getDADataSource());
//		String sql="update score set name=?,score=? where course=? ";
//		int cnt=queryRunner.update(sql,new Object[]{"溜溜","200","python"});
//		if(cnt>0) {
//			System.out.println("修改成功");
//		}else {
//			System.out.println("修改失败");
//		}
//	}
}

c3p0-config.xml(c3p0配置)


<c3p0-config>
	<default-config>
		<property name="driverClass">com.mysql.jdbc.Driverproperty>
		<property name="jdbcUrl">jdbc:mysql://localhost:3308/web_login?characterEncoding=utf8property>
		<property name="user">rootproperty>
		<property name="password">3308property>
		
		<property name="initialPoolSize">5property>
		<property name="minPoolSize">5property>
		<property name="maxPoolSize">20property>
	default-config> 
c3p0-config>

com.item.domain(封装的实体类,登录,成绩)
Student.java

package com.item.domain;

public class Student {
	private String sno;
	private String pwd;
	public String getSno() {
		return sno;
	}
	public void setSno(String sno) {
		this.sno = sno;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	
}

Studentscore.java

package com.item.domain;

public class Studentscore {
	private String name;
	private String course;
	private int score;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCourse() {
		return course;
	}
	public void setCourse(String course) {
		this.course = course;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	
}

你可能感兴趣的:(java)