ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java web基础-文章发表

2022-07-17 15:09:27  阅读:174  来源: 互联网

标签:userName web java String request nbsp 文章 import public


这个是基于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.效果展示:

标签:userName,web,java,String,request,nbsp,文章,import,public
来源: https://www.cnblogs.com/fzlgxy085/p/16486844.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有