本文目录一览:
jsp程序代码
%@ page language="java" import="java.util.*" errorPage="error.jsp" pageEncoding="UTF-8"%
% request.setCharacterEncoding("GBK");%
html
head
title动作处理页面/title
/head
body background="C:\tp\yy.JPG"
font color=yellow
centerbrh2
jsp:useBean id="loginbean" class="com.login.LoginBean"/jsp:useBean //使用jsp:useBean标签调用LoginBean.java,class指明JavaBean位置
jsp:setProperty name="loginbean" property="*" ///设置LoginBean的属性值
%out.println("用户号为:"+loginbean.getUserid()+"brbr"); 输出loginbean里面getUserid的值
if (loginbean.getUserid()==null){ //如果用户名为空
throw new Exception("尊敬的用户:系统登录页面中的用户名不能为空,请输入用户号!");
}
if (loginbean.getPsw()==null){ //如果密码为空
throw new Exception("尊敬的用户:系统登录页面中的密码不能为空,请输入密码!");
}
if (loginbean.getPsw().equalsIgnoreCase("neusoft")) //如果JavaBean的密码和输入密码相同
{
String puserid = request.getParameter("userid"); //获得此用户ID
Cookie cookies[] = request.getCookies();
//如果是上一步提交过来的,则更新Cookie的值
if(puserid != null) {
Cookie c = new Cookie("userid", puserid);
c.setMaxAge(60*60*24*30); //Cookie的有效期为1个月setMaxAge()是Cookie 的一个方法
器单位为秒
60*60*24*30等与30天*24小时*60分*60秒==结果就为1个月的秒数
response.addCookie(c);
}
session.setAttribute("login","ok");
session.setMaxInactiveInterval(-1);
%
jsp:forward page="success.jsp" / //跳转至success.jsp
%
}
else { //如果密码不正确则执行
out.println("对不起,密码输入错误!您此次系统登陆失败!请重新输入密码!brbr");
out.println("A href=login.jsp返回/Abr");
}
%
h2
br
hr
/font
/body
/html
答题完毕
需要一个可以运行的JSP简单代码?
%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%
%
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%"
titleMy JSP 'index.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
This is my JSP page. br
/body
/html
请帮我看一个简单的JSP程序,谢谢!
out.print("img src = bar.jpg width ='%=a[i]*5 % 'height=%=10*a[i]%‘");
还不对的话 百度HI
因为out是jsp的内置对象里面本来就是写的java代码 而%%的话 只是用与html代码段插入java代码
%%中可以插入out代码 但是反过来就不行了
很多问题自己没遇到过 一看是看不出的 这就是所谓的经验 呵呵 这就是一再强调自己动手的原因
临时急用,大家帮我写短简单的JSP代码
b.jsp
%
session.setAttribute("SerachPath","b.jsp") ;
%
c.jsp
%
session.setAttribute("SerachPath","c.jsp") ;
%
a.jsp
%
String validate = (String)session.getAttribute("SerachPath") ;
if(validate == null){
session.setAttribute("SerachPath","b.jsp")
}else if(validate.equels('b.jsp')){
System.out.println(validate);
}else if(validate.equels("c.jsp")){
%
script
alert("aaaa");
/script
%
}
%
求大神写一下jsp的简单的注册界面代码。
1.需要一个jsp页面:
//login.jsp核心代码:
form action="${pageContext.request.contextPath}/servlet/UserServlet" method="post"
input type="text" name="loginname" /input type="password" name="password"/
input type="submit" value="登录"/
/form
2.需要一个servlet来验证登录信息
//UserServlet 核心代码
class UserServlet extends HttpServlet{
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
process(request, response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
process(request, response);
}
private void process(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
PrintWriter pw = response.getWriter();
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
String loginname = request.getParameter("loginname");
String password = request.getParameter("password");
//创建一个service来处理业务逻辑(包括查询数据库操作)
UserService service = new UserService();
boolean bool = service.validateUser(loginname,password);
if(!bool){
pw.println("用户名或密码错误");
}else{
pw.println("登录成功");
}
}
3.需要一个service处理业务逻辑(包括查询数据库操作)
//UserService 核心代码
public class UserService{
/**
*查询数据库验证用户是否存在,返回boolean
*/
public boolean validateUser(String loginname,String password){
boolean bool = false;
Connection conn = null;
PreparedStatement ps = null;
//这里以mysql为例
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
String sql = "select login_name,pass_word from t_user where login_name=? and pass_word=?";
ps = conn.prepareStatement(sql);
ps.setString(0, loginname);
ps.setString(1, password);
ResultSet rs = ps.executeQuery();
if(rs.next()){
bool = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(conn != null){
conn.close();
conn = null;
}
if(ps != null){
ps.close();
ps = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return bool;
}
}