㈠ 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>
<td><input 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" "http://www.w3.org/TR/html4/loose.dtd">
<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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert 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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>登录界面</title>
</head>
<body background="C:Userswin8workspaceLoginimage\_10.jpg" >
<center>
<br><br><br><br><br><br>
<h1 style="color:yellow">Login</h1>
<br>
<form name="loginForm" action="login_check.jsp" method="post">
<table Border="0" >
<tr >
<td>账号</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password">
</td>
</tr>
</table>
<br>
<input type="submit" value="登录" style="color:#BC8F8F">
</form>
</center>
</body>
</html>
㈡ 缃戠珯jsp婧愮爜鏄浠涔堟剰镐濓纻
缃戠珯JSP婧愮爜鏄涓绉嶅姩镐佺绣椤电紪绋嬭瑷锛屽畠镄勫叏绉版槸Java Server Pages锛屽畠鍏佽稿湪Java浠g爜涓宓屽叆HTML镙囱帮纴浠ヤ究鐢ㄤ簬瀹㈡埛绔娴忚埚櫒璁块梾鍜屾樉绀恒侸SP婧愮爜涓昏佺敤浜庡紑鍙慦eb搴旂敤绋嫔簭鍜屼紒涓氱骇搴旂敤绋嫔簭锛屽彲浠ユ柟渚垮湴杩涜屽紑鍙戝拰缁存姢銆傚畠涓虹▼搴忓憳鎻愪緵浜嗛潪甯哥伒娲荤殑寮鍙戝钩鍙帮纴骞跺彲浠ヨ交𨱒惧湴鐢ㄤ簬鏋勫缓楂樻晥鍜屽畨鍏ㄧ殑Web搴旂敤绋嫔簭銆
JSP婧愮爜鍏锋湁璁稿氱壒镣瑰拰鐢ㄩ斻傞栧厛锛屽畠鎻愪緵浜嗕竴涓绫讳技浜嶩TML镄勭紪绋嬫ā鍨嬶纴绋嫔簭锻桦彲浠ユ洿杞绘涧鍦颁娇鐢ㄧ粍浠躲佹爣璁板拰鑴氭湰𨱒ユ瀯寤哄嶆潅镄刉eb搴旂敤绋嫔簭銆傚叾娆★纴JSP婧愮爜鏄锘轰簬Java镄勶纴杩欐剰锻崇潃搴旂敤绋嫔簭鍙浠ュ湪涓嶅悓镄勫钩鍙颁笂杩愯岋纴钥屼笖寰埚规槗娣诲姞鍜屽垹闄ゅ姛鑳姐傝缮鍙浠ヤ娇鐢↗ava绫绘潵镎崭綔鏁版嵁搴揿拰鏂囦欢銆傛ゅ栵纴JSP婧愮爜涔熸敮鎸丣avaBean锛岃繖浣垮缑绋嫔簭锻桦彲浠ユ洿杞绘涧鍦板皝瑁呮暟鎹鍜屽姛鑳戒互渚块吨鐢ㄣ
JSP婧愮爜镄勫簲鐢ㄨ寖锲撮潪甯稿箍娉涳纴瀹冭鐢ㄤ簬璁稿氢紒涓氱骇搴旂敤绋嫔簭鍜屼簰镵旂绣搴旂敤绋嫔簭镄勬瀯寤轰腑銆备緥濡傦纴鐢靛瓙鍟嗗姟銆佺ぞ浜ょ绣缁溿佸湪绾块摱琛屽拰淇濋橹绛夊悇绉嶅簲鐢ㄧ▼搴忛兘浣跨敤JSP婧愮爜杩涜屽紑鍙戙傛湭𨱒ワ纴闅忕潃浜戣$畻鍜岀墿镵旂绣鎶链镄勪笉鏂鍙戝𪾢锛孞SP婧愮爜镄勫簲鐢ㄥ拰闇姹备篃灏嗙户缁澧炲姞銆傚逛簬绋嫔簭锻樻潵璇达纴镡熺粌鎺屾彙JSP婧愮爜灏嗘槸涓椤归潪甯告湁鍓嶉旂殑鎶鑳姐
㈢ 给定JSP程序源码如下:
a和d是表达式,所以有输出;c和d是java程序片,要想输出得用java的输出,所以这里没有输出。
a是先加后输出,d是先输出后加,所以一个是2,一个是1.
㈣ jsp实现自动发送电子邮件的源代码
package com.sinosoft.reins.pengwei;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
* 简单邮件(不带附件的邮件)发送器
*/
public class SimpleMailSender {
/**
* 以文本格式发送邮件
* @param mailInfo 待发送的邮件的信息
*/
public boolean sendTextMail(MailSenderInfo mailInfo) {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
try {
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO,to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
// 发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
/**
* 以HTML格式发送邮件
* @param mailInfo 待发送的邮件信息
*/
public static boolean sendHtmlMail(MailSenderInfo mailInfo){
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
//如果需要身份认证,则创建一个密码验证器
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
try {
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
// Message.RecipientType.TO属性表示接收者的类型为TO
mailMessage.setRecipient(Message.RecipientType.TO,to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
// 设置HTML内容
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
// 发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
}
㈤ JSP锅氢竴涓璁$畻灏忕▼搴忥纴姹傛渶钖庝竴涓璇鍙ワ紒宸叉湁婧愪唬镰佸拰鎴愬搧锲剧墖锛佸氨宸链钖庨偅涓缁撴灉镄勮鍙ヤ简銆傜湡蹇冩眰瑙fョ敤銆
Q5Action.jsp
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
String cashAmount = request.getParameter("cashamount");
String taxAmount = "";
if (cashAmount != null && cashAmount.length() > 0) {
int cash = Integer.parseInt(cashAmount);
if (cash > 0 && cash <= 10000) {
taxAmount += cash * 0.0205;
} else if (cash > 10000 && cash <= 30000) {
taxAmount += cash * 0.0215;
} else if (cash > 30000) {
taxAmount += cash * 0.0250;
}
}
%>
<html>
<head>
<title>ST8016 Assignment Q5</title>
<script type="text/javascript">
function verify() {
regExp = /\d+\b/;
if (!(regExp.test(thisForm.cashamount.value))) {
alert("You must enter valid amount!");
return false;}
return true;
}
</script>
</head>
<body>
<h1>Assignment 1 - Question 5</h1>
<b>Tax Calculation</b>
<br /><br />
<table border="1" cellpadding="3">
<tr bgcolor="#EEEEEE"><td align="center">Amount</td><td align="center">Tax Rate %</td></tr>
<tr><td align="center">1 - 10,000 </td><td align="center">2.05 % (0.0205)</td></tr>
<tr><td align="center">10,001 - 30,000 </td><td align="center">2.15 % (0.0215)</td></tr>
<tr><td align="center">> 30,000 </td><td align="center">2.50 % (0.0250)</td></tr>
</table>
<form name="thisForm" action="Q5Action.jsp" method=钬漰ost钬
onsubmit="return verify();">
Amount: <input name=cashamount size=10 value="<%=cashAmount%>"/> <br/><br/>
<input type=submit value="Calculate Tax Amount"/>
</form>
<% if (taxAmount.length() > 0) {%>
<p>Tax Amount: <b><%=taxAmount%></b></p>
<% }%>
</body>
</html>
㈥ jsp投票系统求源代码!可以连数据库的!
<p style=line-height: 150%><SPAN style="FONT-SIZE: 12px"><STRONG>这是用文本文件作为存储载体的投票系统:</STRONG></SPAN>
<p style=line-height: 150%><SPAN style="FONT-SIZE: 12px"><STRONG>vote.java:</STRONG></SPAN>
<SPAN style="FONT-SIZE: 12px"><STRONG></STRONG> <p style=line-height: 150%>
// Java Document
package vote;
import java.io.*;
import java.util.*;
public class vote extends Object
{
public String filePath="";
public int n;
private File voteFile;
private BufferedReader fileRead;
private PrintWriter fileWrite;
public String systemMessage="";
private String voteStr[]=new String[10];
public int voteNum[]=new int[10];
public void createFile()
throws FileNotFoundException
{
voteFile=new File(filePath);
if(!voteFile.exists())
{
fileWrite=new PrintWriter(new FileOutputStream(filePath));
for(int i=0;i<n;i++) fileWrite.println("0");
fileWrite.close();
}
}
public void writeFile()
throws FileNotFoundException
{
fileWrite=new PrintWriter(new FileOutputStream(filePath));
for(int i=0;i<n;i++)
{
fileWrite.println(voteNum[i]);
}
fileWrite.close();
}
public void readFile()
throws FileNotFoundException
{
fileRead=new BufferedReader(new FileReader(filePath));
for(int i=0;i<n;i++)
{
try
{voteStr[i]=fileRead.readLine();
}
catch(IOException f)
{
voteStr[i]="0";}
voteNum[i]=Integer.parseInt(voteStr[i]);
}
try
{
fileRead.close();
}
catch(IOException d)
{
systemMessage=d.toString();
}
}
}
<p style=line-height: 150%><SPAN style="LINE-HEIGHT: 15pt"><SPAN style="FONT-SIZE: 12px"><STRONG>vote.jsp:</STRONG>
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%@ page import="java.util.*"%>
<%@ page import="java.lang.*"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="vote" scope="request" class="vote.vote"/>
<%
String vote1=request.getParameter("lang");
vote.n=4;
vote.filePath="vote.txt";
vote.createFile();
vote.readFile();
if(vote1.compareTo("0")==0)
vote.voteNum[0]++;
if(vote1.compareTo("1")==0)
vote.voteNum[1]++;
if(vote1.compareTo("2")==0)
vote.voteNum[2]++;
if(vote1.compareTo("3")==0)
vote.voteNum[3]++;
vote.writeFile();
%>
<script language="javascript">
alert("感谢你投了宝贵的一票");
self.location="index.jsp";
</script></SPAN></SPAN>
<p style=line-height: 150%><STRONG> </STRONG><SPAN style="LINE-HEIGHT: 15pt"><SPAN style="FONT-SIZE: 12px"><STRONG>see.jsp:</STRONG>
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%@ page import="java.util.*"%>
<%@ page import="java.lang.*"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="vote" scope="request" class="vote.vote"/>
<%
String vote1=request.getParameter("lang");
vote.n=4;
vote.filePath="vote.txt";
vote.createFile();
vote.readFile();
int total=0;
float voteFlo[]=new float[5];
for(int i=0;i<4;i++) total+=vote.voteNum[i];
for(int i=0;i<4;i++) voteFlo[i]=150*((float)vote.voteNum[i]/(float)total);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>查看调查</title>
<link href="t1.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="30%" border="0" class="t1">
<tr>
<td colspan="2"><div align="center">调查结果</div></td>
</tr>
<tr>
<td width="18%">JSP</td>
<td width="82%"><img src="bar.gif" width=<%=voteFlo[0]%> height=8> <%=vote.voteNum[0]%></td>
</tr>
<tr>
<td>ASP</td>
<td><img src="bar.gif" width=<%=voteFlo[1]%> height=8> <%=vote.voteNum[1]%></td>
</tr>
<tr>
<td>PHP</td>
<td><img src="bar.gif" width=<%=voteFlo[2]%> height=8> <%=vote.voteNum[2]%></td>
</tr>
<tr>
<td>其他</td>
<td><img src="bar.gif" width=<%=voteFlo[3]%> height=8> <%=vote.voteNum[3]%></td>
</tr>
<tr>
<td colspan="2"><div align="center"><a href="javascript:window.close();">关闭窗口</a></div></td>
</tr>
</table>
</body>
</html>
</SPAN></SPAN>
<p style=line-height: 150%><SPAN style="LINE-HEIGHT: 15pt"><SPAN style="FONT-SIZE: 12px"><SPAN style="LINE-HEIGHT: 15pt"><SPAN style="FONT-SIZE: 12px"><STRONG>index.jsp:</STRONG>
</SPAN></SPAN>
</SPAN></SPAN> <p style=line-height: 150%><SPAN style="LINE-HEIGHT: 15pt"><SPAN style="FONT-SIZE: 12px"><%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>投票</title>
<link href="t1.css" rel="stylesheet" type="text/css">
</head>
<script language="javascript">
function cw()
{window.open("see.jsp","mywindow","toolbar=no,left=150,top=200,width=270,height=350,menubar=no,systemMenu=no");
}
</script>
<body>
<table width="15%" height="250" align="left">
<tr>
<td><form name="form1" method="post" action="vote.jsp">
<table width="100%" height="250" border="1" align="center" bordercolor="#9966CC" class="t1">
<tr>
<td><div align="left">你所使用的开发语言</div></td>
</tr>
<tr>
<td><input type="radio" name="lang" value="0">
JSP</td>
</tr>
<tr>
<td><input type="radio" name="lang" value="1">
ASP</td>
</tr>
<tr>
<td><input type="radio" name="lang" value="2">
PHP</td>
</tr>
<tr>
<td><input type="radio" name="lang" value="3">
其他 </td>
</tr>
<tr>
<td><div align="center">
<input name="vote" type="image" src="poll.gif" width="40" height="20" border="0">
<a href="javascript:cw()"><img src="see.gif" width="40" height="20" border="0"></a></div></td>
</tr>
</table>
</form></td>
</tr>
</table>
</body>
</html>
</SPAN></SPAN></SPAN>
㈦ JSP 源代码编写 表单操作
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
<body>
<form action="" method="post">
第一个操作数:<input type="text" name="one"><br>
运算符:<select name="op">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select><br>
第二个操作数:<input type="text" name="two"><br>
<input type="submit" value="提交">
</form>
<hr>
<%
int ones=0;
int twos=0;
double threes=0.0;
String one=request.getParameter("one");
ones=Integer.parseInt(one);
String op=request.getParameter("op");
String two=request.getParameter("two");
twos=Integer.parseInt(two);
out.println(ones);
out.println(op);
out.println(twos);
out.println("=");
if(op=="+")
{
threes=(double)ones+twos;
out.println(threes);
}
else if(op.equals("-"))
{
threes=(double)ones-twos;
out.println(threes);
}
else if (op.equals("/"))
{
if(twos!=0)
{
threes=(double)ones/twos;
out.println(threes);
}
else
out.println("输入的数据有误");
}
else
{
threes=(double)ones*twos;
out.println(threes);
}
%>
</body>
</html>