java web基础-文章发表


这个是基于Java的登陆进行的优化和增加的代码的页面。并且还加入了Java web 的servlet用法

  1. longin登陆页面的代码:
点击查看代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page language="java" import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>员工登录入口</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">
<style type="text/css">
h1{
text-align:center;
padding-top:40px;
}
body{
width:1100px;
text-align:center;
margin-left:auto;
margin-right:auto;
}
.div_01{
background-color:pink;
border:2px soild #aaaaaa;
width:1100px;
height:100px;
font-size:20pt;
line-height:100%;
}
</style>
</head>
<body>
<div class="div_01">
<h1>员工登录入口</h1>
</div>
<div class="div_02">
<form action="${pageContext.request.contextPath}/LoginServlet" method="post">
<br><br><br>
用户名:<input type="text" name="userName" style="background-color:Aquamarine;"><br><br>
密&nbsp;码:&nbsp;&nbsp;<input type="password" name="userPwd" style="background-color:Aquamarine;"><br><br>
<input type="submit"  value="登录" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset" value="重置"><br>
</form>
</div>
</body>
</html>

2.longinServlet代码:

点击查看代码
 
package cn.cszyedu.servlet;

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 javax.servlet.http.HttpSession;

import cn.cszyedu.po.User;
/**
 * Servlet implementation class LoginServlet
 */
@WebServlet(description="/LoginServlet",urlPatterns={"/LoginServlet"})
@SuppressWarnings("unused")
public class LoginServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		String userName=request.getParameter("userName");
		String userPwd=request.getParameter("userPwd");
		HttpSession session=request.getSession();
		if(userName==null||userPwd==null){
			request.setAttribute("message","用户名或密码为空");
			
			response.sendRedirect(request.getContextPath()+"login.jsp");
		}else if(userName.equals("lmy" )&& userPwd.equals("123")){
			session.setAttribute("userName", userName);
			session.setAttribute("userPwd",userPwd);
			
			
			request.getRequestDispatcher("user2.jsp").forward(request, response);
		}else{
			//request.setAttribute("message","用户名或密码错误");
			response.sendRedirect(request.getContextPath()+"login.jsp");
		}
	}
}

3.主页面代码:

点击查看代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page language="java" import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="pagema" 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">
<title>员工之窗</title>
<style>
h1{
text-align:center;
padding-top:40px;
}
.div_01{
	background-color:pink;
	border:2px soild #aaaaaa;
	width:1100px;
	height:100px;
	font-size:20pt;
	line-height:100%;
}
.div_02{
	text-align:left;
	line-height:20pt;
	padding-top:10px;
	padding-borrom:10px;
	font-weight:bold;
	border-bottom:soild #777777 2px;;
}
.div_03{
	padding-top:10px;
}
.span_01{
	color:red;
	}
#span_02{
	width:300px;
	height:40px;
	background-color:red;
}
#th_01{
	background-color:#22ccff;
	width:140px;
	height:60px;
}
a{
	font-family:宋体;
	text-align:left;
	text-frcoration:underline;
	TEXT-DECORATION:none;
	}
#H{
	margin-left:500px;
}
</style>
</head>
<body>
<div class="div_01">
	<h1>员工之窗</h1>
</div>
<jsp:useBean id="user" class="cn.cszyedu.po.User" scope="session"></jsp:useBean>
<jsp:useBean id="date" class="java.util.Date"/>
<jsp:setProperty name="user" property="*"/>
<div class="div_02">
	<span class="span_01"><jsp:getProperty name="user" property="userName"/></span>欢迎来到员工之窗
	<br/>
	当前时间:<div id="span_02"><%= date %></div>
	<hr>
</div>
<div class="div_03">
	<table border="1" cellspacing="0" align="center">
		<tr>
			<th id="th_01">文章编号</th>
			<th id="th_01">文章类型</th>
			<th id="th_01">标题</th>
			<th id="th_01">作者</th>
			<th id="th_01">发表日期</th>
		</tr>
	</table><br>
	<div id="H">
		<a href="${pagrContext.request.contextPath}/lmy-sy4/publish.jsp">【发表文章】</a>&nbsp;&nbsp;&nbsp;
		<a href="${pageContext.request.contextPath}/out.jsp">【退出登录】</a>&nbsp;&nbsp;&nbsp;
		<a href="${pageContext.request.contextPath}/login.jsp">【返回首页】</a></div>
</div>
	</div>
</body>
</html>

4.发表文章代码:

点击查看代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
table{
border-color:Aquamarine;
}
.td_01{
width:500px;
height:50px;
text-align:center;
font-weight:bold;
font-size:20pt;
background-color:pink;
color:#222222;

}
.td_02{
width:180px;
text-align:center;
}
td_03{
height:100px;
text-align:center;
}
</style>
</head>
<body>
	<form  action="/serlvet/PublishServlet" method="post">
		<table border="1" cellspacing="0" align="center">
			<tr>
				<td colspan="2" class="td_01">发表文章</td>
			</tr>
			<tr>
				<td class="td_02">文章类型:</td>
				<td>
					<select name="articType">
						<option value="程序设计">程序设计</option>
						<option value="军事">军事</option>
						<option value="艺术设计">艺术设计</option>
						<option value="传统文化">传统文化</option>
						<option value="篮球体育">篮球体育</option>
						<option value="世界地图">世界地图</option>
					</select>
				</td>
			</tr>
			<tr>
			<td class="td_02">文章标题:</td>
			<td>
			 <input type="text" name="articTitle"/>
			</td>
			</tr>
			<tr>
			<td class="td_03">文章内容</td>
			<td>
			<textarea rows="10" cols="41" name="articContent"></textarea>
			</td>
			</tr>
			<tr>
			<td class="td_02">您已发表的文章数:</td>
			<td>${sessionScope.count}篇</td>
			</tr>
			<tr>
			<td colspan="2" align="center">
			<input type="submit" value="确定发表">&nbsp;&nbsp;
			<input type="button" onclick="out()" value="返回首页">&nbsp;&nbsp;
			<input type="button" onclick="back()" value="返回用户界面">
			</td>
			</tr>
		</table>
	</form>
	<script type="text/javascript">
	function out(){
	window.location.href="../experiment/login.jsp";
	}
	//var path=${pageContext.request.contextPath};
	function back(){
	window.location.href="./user2.jsp";
	}
	</script>
</body>
</html>

5.文章的判断代码:

点击查看代码

package cn.cszyedu.servlet;

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 java.io.PrintWriter;
import java.util.Date;
import cn.cszyedu.po.Articles;
/**
 * Servlet implementation class PublishServlet
 */
@WebServlet(description="/PublishServlet",urlPatterns={"/PublishServlet"})
public class PublishServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		
		//获取参数
		String articType=request.getParameter("articType");
		String articTitle=request.getParameter("atricTitle");
		String articContent=request.getParameter("articContent");
		//User user=(User) request.getSession().getAttribute("user");
		//String articAuthor= user.getUserName();
		if(articTitle==null||articTitle==""||articContent==null||articContent==""){
			request.setAttribute("message","文章标题和文章内容不能为空");
			request.getRequestDispatcher("new_experiment/publish.jsp").forward(request,response);
		}else{
			Articles article=new Articles(null,articType,articTitle,articContent,
					request.getSession().getAttribute("userName").toString(),new Date());
			request.setAttribute("article",article);
			request.getRequestDispatcher("new_experiment/user2.jsp").forward(request,response);
			//response.sendRedirect(request.getContextPath()+"jsp/user.jsp");
		}
		//System.out.println(atricType);
	}

}

6.实例化代码:

点击查看代码
package cn.cszyedu.po;
public class User {
	private String userName;
	private String userPwd;
	private String eMail;
	private String telephone;
	private Integer loginTimes;
	public User(){
		super();
	}
	public User(String userName,String userPwd,String eMail,
			String telephone,Integer loginTimes){
		super();
		this.userName=userName;
		this.userPwd=userPwd;
		this.eMail=eMail;
		this.telephone=telephone;
		this.loginTimes=loginTimes;
	}
	public String getUserName(){
		return userName;
	}
	public void setUserName(String userName){
		this.userName=userName;
	}
	public String geteMail(){
		return eMail;
	}
	public void seteMail(String eMail){
		this.eMail=eMail;
	}
	public String getTelephone(){
		return telephone;
	}
	public void setTelephone(String telephone){
		this.telephone=telephone;
	}
	public Integer getLoginTimes(){
		return loginTimes;
	}
	public void setLoginTimes(Integer loginTimes){
		this.loginTimes=loginTimes;
	}
	@Override
	public String toString(){
		return "User[userName="+userName+",userPwd="+userPwd+",eMail="+eMail+","
				+ "telephone="+telephone+",loginTimes="+loginTimes+"]";
	}
}

7.效果展示:

java web基础-文章发表

java web基础-文章发表

java web基础-文章发表

原创文章,作者:,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/274993.html

(0)
上一篇 2022年7月17日 20:03
下一篇 2022年7月17日 20:04

相关推荐

发表回复

登录后才能评论