JSP 08课

Posted naoguakerteng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSP 08课相关的知识,希望对你有一定的参考价值。

技术图片

技术图片

<%@page import="com.gd.entity.Msg"%>
<%@page import="com.gd.dao.MsgDao"%>
<%@page import="com.gd.entity.Users"%>
<%@ 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>
<base href="<%=basePath%>">

<title>My JSP main.jsp starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
</head>

<body>
<%
    Users u = (Users) session.getAttribute("user");
    MsgDao md=new MsgDao();
    List<Msg> list=md.getMailByReceiver(u.getUsername());
%>
<a href="editemail.jsp">写邮件</a>
<table border="0" cellpadding="0" cellspacing="0" align="center" width="750px">
<td width="100px" height="35px">username</td>
    <td width="100px" height="35px">title</td>
      <td width="150px" height="35px">msgcontent</td>
        <td width="100px" height="35px">state</td>
          <td width="100px" height="35px">sendto</td>
            <td width="200px" height="35px">msg_create_date</td>

<%
for(int i=0;i<list.size();i++){
 %>
 <tr>
 <td><%=list.get(i).getMsgid() %></td>
<td><%=list.get(i).getUsernname() %></td>
<td><a href="detail.jsp?id=<%=list.get(i).getMsgid()%>"><%=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"></img>
<%}else{ %>
<img src="images/sms_readed.png"></img>
<%} %>


</td><!-- 0已读,1未读 -->
<td><%=list.get(i).getMsg_create_date() %></td>
 <td><a href="">回复</a></td>
  <td><a href="">删除</a></td>
 
 </tr>
 
 
 
 <%} %>


</talbe>


</body>
</html>
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //加载驱动
            String url = "jdbc:mysql://localhost:3306/jdbcjsp";
            String user = "root";
            String password = "root";
            conn = DriverManager.getConnection(url, user, password);
            // 连接对象
            //conn = BaseDao.getConnection();
    %>
    
    <form action="insert.jsp" name="huifu" method="post">
        用户:<input type="text" name="sendtoid"><br> <br> 
        
        标题:<input type="text" name="ctitle"><br> <br> 
        
        正文:<input type="text" name="ccontent"><br>
        <br>
        <input type="submit" value="发送">
        <a href="main.jsp">返回</a>
    </form>
    <%
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //BaseDao.closeAll(conn, ps, rs);
            try {
                if (rs != null) {
                    rs.close();
                }
            } finally {
                try {
                    if (ps != null) {
                        ps.close();
                    }
                } finally {
                    if (conn != null) {
                        conn.close();
                    }
                }
            }
        }
    %>
  </body>
</html>
<%@ page language="java" import="java.util.*" import="java.sql.*" import="com.gd.entity.Msg" 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>
    <base href="<%=basePath%>">
    
    <title>My JSP insert.jsp starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
   <%
        request.setCharacterEncoding("utf-8");
        String uuname = (String) session.getAttribute("uuname");
        String ctitle = request.getParameter("ctitle");
        String ccontent = request.getParameter("ccontent");
        String sendtoid = request.getParameter("sendtoid");
        //获取邮件列表页面输入的标题
        String contitle = (String) session.getAttribute("newcontitle");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //加载驱动
            String url = "jdbc:mysql://localhost:3306/jdbcjsp";
            String user = "root";
            String password = "root";
            conn = DriverManager.getConnection(url, user, password);
            // 连接对象
            //conn = BaseDao.getConnection();
    %>
    <%
    if(contitle!=null){
        Msg msg = new Msg();
            ps = conn.prepareStatement("select * from msg where title=?and usernname=?");
            ps.setString(1, contitle);
            ps.setString(2, uuname);
            rs = ps.executeQuery();
            while (rs.next()) {
            ps = conn.prepareStatement("insert into msg(usernname,title,msgcontent,sendto,state,msg_create_date)" + "values(‘"
                    +sendtoid + "‘,‘" + ctitle + "‘,‘" + ccontent + "‘,‘"
                    + uuname + "‘,‘" + "1" + "‘,‘"+"2020-04-27"+"‘)");
                    ps.executeUpdate();
                    request.getRequestDispatcher("index.jsp").forward(request, response);
    %>
    <%
        }
        }else{
        Msg msg = new Msg();
            
        
            ps = conn.prepareStatement("insert into msg(usernname,title,msgcontent,sendto,state,msg_create_date)" + "values(‘"
                    + sendtoid + "‘,‘" + ctitle + "‘,‘" + ccontent + "‘,‘"
                    + uuname + "‘,‘" + "1" + "‘,‘"+"2020-04-27"+"‘)");
                    ps.executeUpdate();
                    request.getRequestDispatcher("index.jsp").forward(request, response);
                    
                    }
    %>
    <%
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //BaseDao.closeAll(conn, ps, rs);
            try {
                if (rs != null) {
                    rs.close();
                }
            } finally {
                try {
                    if (ps != null) {
                        ps.close();
                    }
                } finally {
                    if (conn != null) {
                        conn.close();
                    }
                }
            }
        }
    %>




    <br>
  </body>
</html>

 

以上是关于JSP 08课的主要内容,如果未能解决你的问题,请参考以下文章

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

在 JSP 中的 <% %> 代码片段中添加链接

使用IDEA学习JSP代码第006课

使用IDEA学习JSP代码第004课

使用IDEA学习JSP代码第007课

使用IDEA学习JSP代码第008课