当前位置:首页 > 代码 > 正文

jsp简单代码(jsp程序设计代码)

admin 发布:2022-12-19 18:38 113


本篇文章给大家谈谈jsp简单代码,以及jsp程序设计代码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

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简单上传代码

servlet文件上传

login.jsp

%@ page language="java" contentType="text/html; charset=gbk"

pageEncoding="gbk"%

!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""

html

head

titleInsert title here/title

/head

body

form action="upload" method="post" enctype="multipart/form-data"

输入用户名input typ="text" name ="username"

input type="file"name="file"/

input type="submit" value="submit"/

/form

/body

/html

result.jsp

%@ page language="java" contentType="text/html; charset=gbk"

pageEncoding="gbk"%

!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""

html

head

title上传结果页面/title

/head

body

username:${requestScope.username }

filename:${requestScope.file }

/body

/html

UploadServlet.java

package com.test.servlet;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadServlet extends HttpServlet {

public UploadServlet() {

}

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

}

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

String path = request.getSession().getServletContext().getRealPath("/upload");

DiskFileItemFactory factory = new DiskFileItemFactory();

factory.setRepository(new File(path));

factory.setSizeThreshold(1024 * 1024);

ServletFileUpload upload = new ServletFileUpload(factory);

try

{

ListFileItemlist = upload.parseRequest(request);

for(FileItem item:list){

if(item.isFormField()){

String name = item.getFieldName();

String value = item.getString("utf-8");

request.setAttribute(name, value);

}

else{

String name = item.getFieldName();

String value = item.getName();

int start = value.lastIndexOf("\\");

String fileName = value.substring(start+1);

request.setAttribute(name, fileName);

System.out.println(fileName);

OutputStream os = new FileOutputStream(new File(path,fileName));

InputStream is = item.getInputStream();

byte[] buffer = new byte[400];

int length = 0;

while((length = is.read(buffer))0){

os.write(buffer,0,length);

}

os.close();

is.close();

}

}

}

catch(Exception e){

e.printStackTrace();

}

request.getRequestDispatcher("/result.jsp").forward(request, response);

}

}

Struts文件上传

1.创建一个工程:

创建一个JSP页面内容如下:

body

form action="uploadAction.do" method="post" enctype="multipart/form-data"

input type="file" name="file"

input type="submit"

/form

/body

2.创建一个FormBean继承ActionForm

其中有个private FormFile file ;属性。FormFile类的全名为:org.apache.struts.upload.FormFile

3.创建一个UploadAction继承自Action

然后重写Action的execute()方法:

代码如下:

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) {

UploadForm uploadForm = (UploadForm) form;

if(uploadForm.getFile()!=null)

FileUtil.uploadFile(uploadForm.getFile(), "e:/abc/accp");

return null;

}

4.创建FileUtil工具类,里面实现上传的文件的方法:

关键代码如下:

public class FileUtil

{

/*** 创建空白文件

* @param fileName 文件名

* @param dir 保存文件的目录

* @return

*/

private static File createNewFile(String fileName,String dir)

{

File dirs = new File(dir);

//看文件夹是否存在,如果不存在新建目录

if(!dirs.exists())

dirs.mkdirs();

//拼凑文件完成路径

File file = new File(dir+File.separator+fileName);

try {

//判断是否有同名名字,如果有同名文件加随机数改变文件名

while(file.exists()){

int ran = getRandomNumber();

String prefix = getFileNamePrefix( fileName);

String suffix = getFileNameSuffix( fileName);

String name = prefix+ran+"."+suffix;

file = new File(dir+File.separator+name);

}

file.createNewFile();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return file;

}

/**

* 获得随机数

* @return

*/

private static int getRandomNumber() {

Random random = new Random(new Date().getTime());

return Math.abs(random.nextInt());

}

/**

* 分割文件名 如a.txt 返回 a

* @param fileName

* @return

*/

private static String getFileNamePrefix(String fileName){

int dot = fileName.lastIndexOf(".");

return fileName.substring(0,dot);

}

/**

* 获得文件后缀

* @param fileName

* @return

*/

private static String getFileNameSuffix(String fileName) {

int dot = fileName.lastIndexOf(".");

return fileName.substring(dot+1);

}

/**

* 上传文件

* @param file

* @param dir

* @return

*/

public static String uploadFile(FormFile file,String dir)

{

//获得文件名

String fileName = file.getFileName();

InputStream in = null;

OutputStream out = null;

try

{

in = new BufferedInputStream(file.getInputStream());//构造输入流

File f = createNewFile(fileName,dir);

out = new BufferedOutputStream(new FileOutputStream(f));//构造文件输出流

byte[] buffered = new byte[8192];//读入缓存

int size =0;//一次读到的真实大小

while((size=in.read(buffered,0,8192))!=-1)

{

out.write(buffered,0,size);

}

out.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

finally

{

try {

if(in != null) in.close();

} catch (IOException e) {

e.printStackTrace();

}

try {

if(out != null) out.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return null;

}

}

一小段jsp代码,很简单

如果你后面用到这个窗口,那么Win可以继续使用。如果肯定不再用,那没完全可以不写!

临时急用,大家帮我写短简单的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、login.jsp文件

%@ page language="java" contentType="text/html; charset=GB18030"

pageEncoding="GB18030"%

%@ page import="java.util.*" %

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

html

head

title登录页面/title

/head

body

form name="loginForm" method="post" action="judgeUser.jsp"

table

tr

td用户名:input type="text" name="userName" id="userName"/td

/tr

tr

td密码:input type="password" name="password" id="password"/td

/tr

tr

tdinput type="submit" value="登录" style="background-color:pink" input

type="reset" value="重置" style="background-color:red"/td

/tr

/table

/form

/body

/html

2、judge.jsp文件

%@ page language="java" contentType="text/html; charset=GB18030"

pageEncoding="GB18030"%

%@ page import="java.util.*" %

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

html

head

title身份验证/title

/head

body

%

request.setCharacterEncoding("GB18030");

String name = request.getParameter("userName");

String password = request.getParameter("password");

if(name.equals("abc") password.equals("123")) {

3、afterLogin.jsp文件

%

jsp:forward page="afterLogin.jsp"

jsp:param name="userName" value="%=name%"/

/jsp:forward

%

}

else {

%

jsp:forward page="login.jsp"/

%

}

%

/body

/html

%@ page language="java" contentType="text/html; charset=GB18030"

pageEncoding="GB18030"%

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

html

head

title登录成功/title

/head

body

%

request.setCharacterEncoding("GB18030");

String name = request.getParameter("userName");

out.println("欢迎你:" + name);

%

/body

/html

扩展资料:

java web登录界面源代码:

1、Data_uil.java文件

import java.sql.*;

public class Data_uil

{

public  Connection getConnection()

{

try{

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

}catch(ClassNotFoundException e)

{

e.printStackTrace();

}

String user="***";

String password="***";

String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=***";

Connection con=null;

try{

con=DriverManager.getConnection(url,user,password);

}catch(SQLException e)

{

e.printStackTrace();

}

return con;

}

public  String selectPassword(String username)

{

Connection connection=getConnection();

String sql="select *from login where username=?";

PreparedStatement preparedStatement=null;

ResultSet result=null;

String password=null;

try{

preparedStatement=connection.prepareStatement(sql);

preparedStatement.setString(1,username);

result=preparedStatement.executeQuery();//可执行的     查询

if(result.next())

password=result.getString("password");

}catch(SQLException e){

e.printStackTrace();

}finally

{

close(preparedStatement);

close(result);

close(connection);

}

System.out.println("找到的数据库密码为:"+password);

return password; 

}

public  void close (Connection con)

{

try{

if(con!=null)

{

con.close();

}

}catch(SQLException e)

{

e.printStackTrace();

}

}

public  void close (PreparedStatement preparedStatement)

{

try{

if(preparedStatement!=null)

{

preparedStatement.close();

}

}catch(SQLException e)

{

e.printStackTrace();

}

}

public  void close(ResultSet resultSet)

{

try{

if(resultSet!=null)

{

resultSet.close();

}

}catch(SQLException e)

{

e.printStackTrace();

}

}

}

2、login_check.jsp:文件

%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%

!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""

html

head

meta http-equiv="Content-Type" content="text/html; charset=utf-8"

title验证用户密码/title

/head

body

jsp:useBean id="util" class="util.Data_uil" scope="page" /

%

String username=(String)request.getParameter("username");

String password=(String)request.getParameter("password");

if(username==null||"".equals(username))

{

out.print("script language='javaScript' alert('用户名不能为空');/script");

response.setHeader("refresh", "0;url=user_login.jsp");

}

else

{

System.out.println("输入的用户名:"+username);

String passwordInDataBase=util.selectPassword(username);

System.out.println("密码:"+passwordInDataBase);

if(passwordInDataBase==null||"".equals(passwordInDataBase))

{

out.print("script language='javaScript' alert('用户名不存在');/script");

response.setHeader("refresh", "0;url=user_login.jsp");

}

else if(passwordInDataBase.equals(password))

{

out.print("script language='javaScript' alert('登录成功');/script");

response.setHeader("refresh", "0;url=loginSucces.jsp");

}

else

{

out.print("script language='javaScript' alert('密码错误');/script");

response.setHeader("refresh", "0;url=user_login.jsp");

}

}

%

/body

/html

3、loginSucces.jsp文件

%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%

!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""

html

head

meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"

titleInsert title here/title

/head

body

hr size="10" width="26%" align="left" color="green"

font size="6" color="red" 登录成功 /font

hr size="10" width="26%" align="left" color="green"

/body

/html

4、user_login.jsp文件

%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%

!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""

html

head

meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"

title登录界面/title

/head

body  background="C:\Users\win8\workspace\Login\image\9dcbdc339e72a5663b5c289fb5573c13_10.jpg"

center

brbrbrbrbrbr

h1 style="color:yellow"Login/h1

br

form name="loginForm" action="login_check.jsp" method="post" 

table Border="0"

tr

td账号/td

tdinput type="text" name="username"/td

/tr

tr

td密码/td

tdinput type="password" name="password"

/td

/tr

/table

br

input type="submit" value="登录" style="color:#BC8F8F"

/form

/center

/body

/html

jsp简单代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于jsp程序设计代码、jsp简单代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/17950.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载