JSP第十三周作业

<%@ page language="java" import="java.util.*" pageEncoding="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 'register.jsp' starting page</title>    
  </head>
 
  <body>
  <script type="text/javascript">
        function validate(){
            if(registerForm.uname.value==""){
                alert("账号不能为空!");
                return;
            }
            if(registerForm.upwd.value==""){
                alert("密码不能为空!");
                return;
            }
            registerForm.submit();
        }
    </script>

    <form  name="registerForm" action="DoregServlet" method="post">

        用户名:<input type="text" name="uname"><br>
        密   码: <input type="password" name="upwd"> <br>
        <input type="submit" value="注册" >
        <a href="denglu.jsp">登录</a>
    </form>
    
  </body>
</html>
public class Dowriteservlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public Dowriteservlet() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        String uname = (String) request.getSession().getAttribute("uname");
        String sendto = request.getParameter("receiver");
        String title = request.getParameter("title");
        String content = request.getParameter("content");

        Msg m = new Msg();
        m.setMsgcontent(content);
        m.setUsername(uname);
        m.setSendto(sendto);
        m.setTitle(title);

        MsgDao md = new MsgDao();
        md.addMsg(m);

        out.print("发送成功.....");
        response.setHeader("refresh", "3;url=main.jsp");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request,response);     }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}
import java.io.IOException;
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 dao.UsersDao;
 
/**
 * Servlet implementation class dologin
 */
@WebServlet("/dologin")
public class dologin extends HttpServlet {
    private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public dologin() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        UsersDao ud=new UsersDao();
        if(ud.login(uname, upwd)){
            request.getSession().setAttribute("uname", uname);
            request.getRequestDispatcher("main.jsp").forward(request, response);
        }else{
            response.getWriter().print("登陆失败,即将跳回登陆页.....");
            response.setHeader("refresh", "5;url=login.jsp");
        }
    }
 
}
package com.jdbc2;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class BaseDao {
    //连接数据库
    public Connection getConnection() {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }
    //关闭数据库
    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();
            }
        }
 
}
<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
    <%
        String uname = (String) session.getAttribute("uname");
        EmailDao dao = new EmailDao();
        List<Email> list = dao.getAll(uname);
    %>
    欢迎<%=uname%>
    <a href="write1.jsp">写邮件</a>
    <a href="exit.jsp">退出登录</a>
    <br>
    <br>
    <table border="1px" cellspacing="0" cellpadding="30px">
        <thead></thead>
        <tr>
            <th>发件人</th>
            <th>标题</th>
            <th>时间</th>
            <th>状态</th>
            <th>操作</th>
            <th>操作</th>
        </tr>
        <tbody></tbody>
        <%
            for (int i = 0; i < list.size(); i++) {
        %>
        <tr>
            <td><%out.print(list.get(i).getFa().toString()); %></td>
            <td><a href="dolook.do?id=<%=list.get(i).getId() %>"><%out.print(list.get(i).getTitle().toString()); %></a></td>
            <td><%out.print(list.get(i).getTime().toString()); %></td>
            <td><%if(list.get(i).getZhuang().equals("0")){
            out.print("<img src='img/2.png'></img>");
            }else{
            out.print("<img src='img/1.png'></img>");
            }%>
            <td><a href="write.jsp?uname=<%=list.get(i).getFa()%>">回复</a></td>
            <td><a href="dodel.do?id=<%=list.get(i).getId()%>">删除</a></td>
             
            </td>
        </tr>
        <%
            }
        %>
    </table>
</body>
</html>

 

posted @ 2022-05-29 20:44  刘源丰  阅读(12)  评论(0编辑  收藏  举报