首页 > 编程知识 正文

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

时间:2023-12-21 10:48:46 阅读:318665 作者:VRKM

本文目录一览:

需要一个可以运行的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:Userswin8workspaceLoginimage9dcbdc339e72a5663b5c289fb5573c13_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;

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。