jsp语言代码示例(jsp程序设计代码)

本文目录一览:

需要一个可以运行的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代码意思

我给你写上注释吧

(1)

//注册驱动

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”).newInstance();

//根据url获得数据库连接

Connection con = DriverManager.getConnection(“jdbc:odbc:internet”,” “, ” “);

//用连接创建startment对象

Statement stmt = con.createStatement();

//获取表单content内容,或者url传来的content参数内容.

String content = request.getParameter(“content”);

//将content转码为GBK编码

content = new String(content。getBytes(“ISO-8859-1”), “GBK”);

//创建日期格式化对象

java.text.DateFormat df = new java.text.SimpleDateFormat(“yyyy-MM-dd- HH:mm”);

//获得当前电脑时间

java.util.Date date = new java.util.Date();

//将日期格式化为字符串,格式为:yyyy-MM-dd- HH:mm

String datestr = df.format(date);

//将datestr转码为GBK编码

datestr = new String(datestr。getBytes(“ISO-8859-1”), “GBK”);

String msg = “您在留言成功。”;

//创建查询sql语句

String sql2 = “select count(*) from liuyan_temp”;

// 查询liuyan_temp表中所有数据的条数

ResultSet rs = stmt.executeQuery(sql2);

int i = 0;

//遍历结果集

while (rs.next())

i = rs.getInt(1); //获得liuyan_temp表记录条数

i = i + 1;

——————————————————————–(2)

try {

//注册驱动

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

}

catch (ClassNotFoundException e) { //捕获类未找到异常

out.print(e); //在jsp页面打印异常信息

}

try {

//创建url信息

String url = “jdbc:odbc:internet”;

//获得数据库连接

conn = DriverManager.getConnection(url, ” “, ” “);

//根据sql语句创建PrepareStatement

ps = conn.prepareStatement(“select * from shipin”);

//执行查询,返回结果集

result = ps.executeQuery();

//遍历结果集

while (result.next()) {

—————————————————————–

(3)

try{

//注册驱动

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

}catch(ClassNotFoundException cnfe){//捕获类未找到异常

out.println(cnfe); //打印异常

}

try{

String url = “jdbc:odbc:internet”;

//创建数据库连接

conn = DriverManager.getConnection(url,””,””);

//根据sql语句创建PrepareStatement

psSelect = conn.prepareStatement(“select * from userInformation where userID=?”);

//为sql语句中第一个问好设置值

psSelect.setString(1,userID);

//执行查询,返回结果集

result = psSelect.executeQuery();

//判断,如果结果集中有查询结果

if(result.next()){%

tr

!– 输出结果集当前行第一个字段的值 —

td%=result.getString(1) %/td

!– 输出结果集当前行第二个字段的值 —

td%=result.getString(2) %/td

/tr

//判断,结果集中没有查询结果

%}else{

//打印JavaScript信息

out.println(“scriptalert(‘查询错误,请重新输入!’)”);

//浏览器的页面url指向manageUser.jsp页面(javascript方法)

out.println(“window.navigate(‘manageUser.jsp’)/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的简单的注册界面代码。

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;

}

}

原创文章,作者:YDCF,如若转载,请注明出处:https://www.506064.com/n/145165.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
YDCFYDCF
上一篇 2024-10-26 11:55
下一篇 2024-10-26 11:55

相关推荐

  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python字符串宽度不限制怎么打代码

    本文将为大家详细介绍Python字符串宽度不限制时如何打代码的几个方面。 一、保持代码风格的统一 在Python字符串宽度不限制的情况下,我们可以写出很长很长的一行代码。但是,为了…

    编程 2025-04-29
  • Python基础代码用法介绍

    本文将从多个方面对Python基础代码进行解析和详细阐述,力求让读者深刻理解Python基础代码。通过本文的学习,相信大家对Python的学习和应用会更加轻松和高效。 一、变量和数…

    编程 2025-04-29
  • AES加密解密算法的C语言实现

    AES(Advanced Encryption Standard)是一种对称加密算法,可用于对数据进行加密和解密。在本篇文章中,我们将介绍C语言中如何实现AES算法,并对实现过程进…

    编程 2025-04-29
  • 仓库管理系统代码设计Python

    这篇文章将详细探讨如何设计一个基于Python的仓库管理系统。 一、基本需求 在着手设计之前,我们首先需要确定仓库管理系统的基本需求。 我们可以将需求分为以下几个方面: 1、库存管…

    编程 2025-04-29
  • 学习Python对学习C语言有帮助吗?

    Python和C语言是两种非常受欢迎的编程语言,在程序开发中都扮演着非常重要的角色。那么,学习Python对学习C语言有帮助吗?答案是肯定的。在本文中,我们将从多个角度探讨Pyth…

    编程 2025-04-29
  • Python满天星代码:让编程变得更加简单

    本文将从多个方面详细阐述Python满天星代码,为大家介绍它的优点以及如何在编程中使用。无论是刚刚接触编程还是资深程序员,都能从中获得一定的收获。 一、简介 Python满天星代码…

    编程 2025-04-29
  • 写代码新手教程

    本文将从语言选择、学习方法、编码规范以及常见问题解答等多个方面,为编程新手提供实用、简明的教程。 一、语言选择 作为编程新手,选择一门编程语言是很关键的一步。以下是几个有代表性的编…

    编程 2025-04-29
  • Python实现简易心形代码

    在这个文章中,我们将会介绍如何用Python语言编写一个非常简单的代码来生成一个心形图案。我们将会从安装Python开始介绍,逐步深入了解如何实现这一任务。 一、安装Python …

    编程 2025-04-29
  • Python被称为胶水语言

    Python作为一种跨平台的解释性高级语言,最大的特点是被称为”胶水语言”。 一、简单易学 Python的语法简单易学,更加人性化,这使得它成为了初学者的入…

    编程 2025-04-29

发表回复

登录后才能评论