jsp10
Posted akaxuan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsp10相关的知识,希望对你有一定的参考价值。
<%-- Created by IntelliJ IDEA. User: xinfeng Date: 2020/5/8 Time: 15:58 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <form action="dologin.jsp" method="post"> 用户名:<input type="text" name="uname"/><br> 密码 :<input type="password" name="upwd"/><br> 验证码 :<input type="text" name="validationCode" size="6"/> <% int intmethod1 = (int) (Math.random() * 10); int intmethod2 = (int) (Math.random() * 10); int intmethod3 = (int) (Math.random() * 10); int intmethod4 = (int) (Math.random() * 10); String intsum = intmethod1 + "" + intmethod2 + intmethod3 + intmethod4; %> <input type="hidden" name="validationCode1" value="<%=intsum%>"> <img src="image1/<%=intmethod1%>.png"> <img src="image1/<%=intmethod2%>.png"> <img src="image1/<%=intmethod3%>.png"> <img src="image1/<%=intmethod4%>.png"><br><br> <input type="submit" name="submit" value="登录"> <input type="reset" value="重置"><br><br> <a href="register.jsp">注册新用户请单击此处</a> </form> </body> </html>
<%-- Created by IntelliJ IDEA. User: xinfeng Date: 2020/5/8 Time: 15:59 To change this template use File | Settings | File Templates. --%> <%@page import="com.gd.dao.UsersDao" %> <%@page import="com.gd.entity.Users" %> <%@ page import="javax.swing.*" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% request.setCharacterEncoding("utf-8"); String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); UsersDao ud = new UsersDao(); if (uname.equals("") || upwd.equals("")) { //用户名或者密码为空的情况 String msg = "用户名或密码不能为空!"; int type = JOptionPane.YES_NO_OPTION; String title = "信息提示"; JOptionPane.showMessageDialog(null, msg, title, type); request.getRequestDispatcher("index.jsp").forward(request, response); } else { //验证码匹配成功 if (request.getParameter("validationCode1").equals( request.getParameter("validationCode"))) { //密码账号登录成功 if (ud.login(uname, upwd)) { Users u = new Users(); u.setUsername(uname); u.setPassword(upwd); session.setAttribute("user", u); request.getRequestDispatcher("main.jsp").forward(request, response); } else { //账号密码不匹配 String msg = "用户名或密码不正确,登录失败!"; int type = JOptionPane.YES_NO_OPTION; String title = "信息提示"; JOptionPane.showMessageDialog(null, msg, title, type); request.getRequestDispatcher("index.jsp").forward(request, response); } } else { String msg = "验证码输入不正确,重新填写!"; int type = JOptionPane.YES_NO_OPTION; String title = "信息提示"; JOptionPane.showMessageDialog(null, msg, title, type); request.getRequestDispatcher("index.jsp").forward(request, response); } } %>
<%-- Created by IntelliJ IDEA. User: xinfeng Date: 2020/5/8 Time: 16:14 To change this template use File | Settings | File Templates. --%> <%@ page import="com.gd.dao.MsgDao" %> <%@ page import="com.gd.entity.Msg" %> <%@ page import="com.gd.entity.Users" %> <%@ page import="java.util.List" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <div align="center" style="font-size: 45px">欢迎登录<% Users u = (Users) session.getAttribute("user"); out.print(u.getUsername()); MsgDao md = new MsgDao(); List<Msg> list = md.getMailByReceiver(u.getUsername()); %></div> <table border="1" width="60%" align="center" style="margin-top: 150px"> <tr> <td>发件人</td> <td>标题</td> <td>收件人</td> <td>状态</td> <td>时间</td> <td> </td> </tr> <% for (int i = 0; i < list.size(); i++) { %> <tr> <td><%=list.get(i).getUsernname() %> </td> <td> <a href="detail.jsp?id=<%=list.get(i).getMsgid()%>&&username=<%=list.get(i).getUsernname()%>"><%=list.get(i).getTitle()%> </a></td> <td><%=list.get(i).getSendto() %> </td> <td><% if (list.get(i).getState() == 1) { %> <img src="images/sms_unReaded.png"/> <%} else { %> <img src="images/sms_readed.png"/> <%} %> </td><!-- 0已读,1未读 --> <td><%=list.get(i).getMsg_create_date() %> </td> <td><a href="delete.jsp?id=<%=list.get(i).getMsgid()%>">删除</a></td> </tr> <%} %> </table> <br> <br> <div align="center"><a href="sendMessage.jsp">发送信息请单击此处</a> <a href="logout.jsp">退出登录请单击此处</a> </div> </body> </html>
<%@ page import="com.gd.dao.MsgDao" %> <%@ page import="com.gd.entity.Msg" %><%-- Created by IntelliJ IDEA. User: xinfeng Date: 2020/5/8 Time: 16:15 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% int id = Integer.parseInt(request.getParameter("id")); String username=request.getParameter("username"); session.setAttribute("username",username); MsgDao md = new MsgDao(); Msg m = md.getMailById(id); md.altstate(id); %> <p> 题目:<%=m.getTitle()%> </p> <p> 来自:<%=m.getUsernname()%> </p> <p> 时间:<%=m.getMsg_create_date()%> </p> <p> 内容:<%=m.getMsgcontent()%> </p> <a href="reply.jsp">回复</a> <a href="main.jsp">返回</a> </body> </html>
<%@ page import="com.gd.entity.Msg" %> <%@ page import="com.gd.dao.MsgDao" %> <%@ page import="javax.swing.*" %><%-- Created by IntelliJ IDEA. User: xinfeng Date: 2020/5/9 Time: 14:13 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% int id = Integer.parseInt(request.getParameter("id")); MsgDao msg = new MsgDao(); if (msg.delete(id)) { String mess = "删除成功!"; int type = JOptionPane.YES_NO_OPTION; String title = "信息提示"; JOptionPane.showMessageDialog(null, mess, title, type); response.sendRedirect("main.jsp"); } %>
<%-- Created by IntelliJ IDEA. User: xinfeng Date: 2020/5/8 Time: 22:49 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="doreply.jsp" method="post"> <p> 题 目:<input type="text" name="title1"> </p> <p> 内 容:<input type="text" name="content"> </p> <input type="submit" value="提交"> <a href="main.jsp">返回</a> </form> </body> </html>
<%@ page import="com.gd.dao.ReplyDao" %> <%@ page import="com.gd.entity.Users" %> <%@ page import="java.sql.PreparedStatement" %> <%@ page import="java.util.Date" %> <%@ page import="javax.swing.*" %> <%-- Created by IntelliJ IDEA. User: xinfeng Date: 2020/5/8 Time: 23:11 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% request.setCharacterEncoding("utf-8"); String title1 = request.getParameter("title1"); String content = request.getParameter("content"); String uname = (String) session.getAttribute("username"); Users u = (Users) session.getAttribute("user"); String sendto = u.getUsername(); ReplyDao Dao = new ReplyDao(); Date utilDate = new Date(); java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); if (title1.equals("") || content.equals("")) { //标题和内容为空的情况 String msg = "标题或内容为空!"; int type = JOptionPane.YES_NO_OPTION; String title = "信息提示"; JOptionPane.showMessageDialog(null, msg, title, type); request.getRequestDispatcher("reply.jsp").forward(request, response); } else { if (Dao.insert(uname, title1, content, sendto, 1, sqlDate)) { request.getRequestDispatcher("reply.jsp").forward(request, response); } } %>
<%-- Created by IntelliJ IDEA. User: xinfeng Date: 2020/5/9 Time: 14:56 To change this template use File | Settings | File Templates. --%> <%@ page import="com.gd.dao.SendMsessage" %> <%@ page import="com.gd.entity.Users" %> <%@ page import="javax.swing.*" %> <%@ page import="java.util.Date" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% request.setCharacterEncoding("utf-8"); String title = request.getParameter("title"); String name = request.getParameter("name"); String content = request.getParameter("content"); Users u = (Users) session.getAttribute("user"); String sendto = u.getUsername(); Date utilDate = new Date(); SendMsessage SM = new SendMsessage(); java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); if (SM.checkinsert(name)) { if (name.equals("") || title.equals("") || content.equals("")) { //标题和内容为空的情况 String msg = "接收人或标题或内容为空!"; int type = JOptionPane.YES_NO_OPTION; String title1 = "信息提示"; JOptionPane.showMessageDialog(null, msg, title1, type); request.getRequestDispatcher("sendMessage.jsp").forward(request, response); } else { if (SM.insert(sendto, title, content,name , 1, sqlDate)) { request.getRequestDispatcher("main.jsp").forward(request, response); } } } else { //没有该联系人 String msg = "无该收件人!"; int type = JOptionPane.YES_NO_OPTION; String title1 = "信息提示"; JOptionPane.showMessageDialog(null, msg, title1, type); response.sendRedirect("sendMessage.jsp"); } %>
<%-- Created by IntelliJ IDEA. User: xinfeng Date: 2020/5/9 Time: 14:56 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="dosendMessage.jsp" method="post"> <p> 接收人:<input type="text" name="name"> </p> <p> 题 目:<input type="text" name="title"> </p> <p> 内 容:<input type="text" name="content"> </p> <input type="submit" value="提交"> <a href="main.jsp">返回</a> </form> </body> </html>
<%-- Created by IntelliJ IDEA. User: xinfeng Date: 2020/5/15 Time: 12:59 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="doregister.jsp" method="post"> 用户名:<input name="username"><br><br> 密 码:<input name="userpassword"><br><br> 邮 箱:<input name="Email"><br><br> <input type="submit" name="submit" value="注册"> <input type="reset" value="重置"><br> </form> </body> </html>
<%@ page import="com.gd.dao.Register" %> <%@ page import="com.gd.entity.Users" %> <%@ page import="javax.swing.*" %> <%-- Created by IntelliJ IDEA. User: xinfeng Date: 2020/5/15 Time: 13:01 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String username = request.getParameter("username"); String userpassword = request.getParameter("userpassword"); String email = request.getParameter("Email"); if (username.equals("") && userpassword.equals("") && email.equals("")) { String msg = "用户名密码邮箱不能为空!"; int type = JOptionPane.YES_NO_OPTION; String title = "信息提示"; JOptionPane.showMessageDialog(null, msg, title, type); response.sendRedirect("register.jsp"); } else { Users users = new Users(); users.setUsername(username); users.setPassword(userpassword); users.setEmail(email); Register register = new Register(); register.register(users); response.sendRedirect("index.jsp"); } %> </body> </html>
<%-- Created by IntelliJ IDEA. User: xinfeng Date: 2020/5/15 Time: 11:34 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% session.invalidate(); response.sendRedirect("index.jsp"); %> </body> </html>
package com.gd.dao; import java.sql.*; public class BaseDao { //获取连接 public Connection getConnection() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); // 2.建立连接 conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/jdbc", "root", "1111"); } catch (Exception e) { e.printStackTrace(); } return conn; } //关闭连接 protected void closeAll(Connection con, PreparedStatement ps, ResultSet rs) { try { if (rs != null) rs.close(); if (ps != null) ps.close(); if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } }
package com.gd.dao; import com.gd.entity.Msg; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class MsgDao extends BaseDao { Connection con = null; ResultSet rs = null; PreparedStatement ps = null; public List<Msg> getMailByReceiver(String receiverName) { List<Msg> list = new ArrayList<Msg>(); try { con = getConnection(); String sql = "select * from msg where sendto=?"; ps = con.prepareStatement(sql); ps.setString(1, receiverName); rs = ps.executeQuery(); while (rs.next()) { Msg msg = new Msg(); msg.setMsgid(rs.getInt("msgid")); msg.setMsgcontent(rs.getString("msgcontent")); msg.setMsg_create_date(rs.getDate("msg_create_date")); msg.setSendto(rs.getString("sendto")); msg.setState(rs.getInt("state")); msg.setTitle(rs.getString("title")); msg.setUsernname(rs.getString("username")); list.add(msg); } closeAll(con, ps, rs); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } public Msg getMailById(int id) { Msg msg = new Msg(); try { con = getConnection(); String sql = "select * from msg where msgid=?"; ps = con.prepareStatement(sql); ps.setInt(1, id); rs = ps.executeQuery(); while (rs.next()) { msg.setMsgid(rs.getInt("msgid")); msg.setMsgcontent(rs.getString("msgcontent")); msg.setMsg_create_date(rs.getDate("msg_create_date")); msg.setSendto(rs.getString("sendto")); msg.setState(rs.getInt("state")); msg.setTitle(rs.getString("title")); msg.setUsernname(rs.getString("username")); } closeAll(con, ps, rs); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return msg; } public boolean delete(int id) { try { con = getConnection(); String sql = "delete from msg where msgid=?"; ps = con.prepareStatement(sql); ps.setInt(1, id); int row = ps.executeUpdate(); if (row > 0) { return true; } } catch (Exception e) { e.printStackTrace(); } finally { closeAll(con, ps, null); } return false; } public void altstate(int id) { try { con = getConnection(); String sql = "update msg set state=0 where msgid=?"; ps = con.prepareStatement(sql); ps.setInt(1, id); ps.executeUpdate(); closeAll(con, ps, null); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.gd.dao; import com.gd.entity.Users; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class Register extends BaseDao { Connection con = null; ResultSet rs = null; PreparedStatement ps = null; public boolean register(Users users) { try { con = getConnection(); String sql = "insert into users(username,PASSWORD,email) values (?,?,?)"; ps = con.prepareStatement(sql); ps.setString(1, users.getUsername()); ps.setString(2, users.getPassword()); ps.setString(3, users.getEmail()); int row = ps.executeUpdate(); if (row > 0) { return true; } else { return false; } } catch (Exception e) { e.printStackTrace(); } finally { closeAll(con, ps, null); } return false; } }
package com.gd.dao; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.SQLException; public class ReplyDao extends BaseDao { Connection conn = null; PreparedStatement ps = null; public boolean insert(String username, String title, String msgcontent, String sendto, int state, Date msg_create_date) throws SQLException { conn = getConnection(); String sql = "insert into msg(username,title,msgcontent,sendto,state,msg_create_date) values (?,?,?,?,?,?)"; ps = conn.prepareStatement(sql); ps.setString(1, sendto); ps.setString(2, title); ps.setString(3, msgcontent); ps.setString(4, username); ps.setInt(5, state); ps.setDate(6, msg_create_date); int row = ps.executeUpdate(); if (row > 0) { closeAll(conn, ps, null); return true; } else { closeAll(conn, ps, null); return false; } } }
package com.gd.dao; import java.sql.*; public class SendMsessage extends BaseDao { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; public boolean checkinsert(String name) throws SQLException { conn = getConnection(); String sql2 = "select * from users where username=?"; ps = conn.prepareStatement(sql2); ps.setString(1, name); rs = ps.executeQuery(); if (rs.next()) { closeAll(conn, ps, rs); return true; } else { closeAll(conn, ps, rs); return false; } } public boolean insert(String username, String title, String msgcontent, String sendto, int state, Date msg_create_date) throws SQLException { conn = getConnection(); String sql = "insert into msg(username,title,msgcontent,sendto,state,msg_create_date) values (?,?,?,?,?,?)"; ps = conn.prepareStatement(sql); ps.setString(1, username); ps.setString(2, title); ps.setString(3, msgcontent); ps.setString(4, sendto); ps.setInt(5, state); ps.setDate(6, msg_create_date); int row = ps.executeUpdate(); if (row > 0) { closeAll(conn, ps, null); return true; } else { closeAll(conn, ps, null); return false; } } }
package com.gd.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class UsersDao extends BaseDao { public boolean login(String uname, String upwd) throws SQLException { Connection conn = getConnection(); String sql = "select * from users where username=? and password=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, uname); ps.setString(2, upwd); ResultSet rs = ps.executeQuery(); if (rs.next()) { closeAll(conn, ps, rs); return true; } else { closeAll(conn, ps, rs); return false; } } }
以上是关于jsp10的主要内容,如果未能解决你的问题,请参考以下文章