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;
}
}
Ⅱ 怎樣用JSP做一個查詢界面
先用DW畫一個界面,上麵包含你要查詢的條件輸入框,還要有一個顯示數據的列表,再加一個按鈕就可以了,最後把擴展名改為.JSP放入工程中,在其裡面寫上相應的java代碼或JSP表達式,實現功能就行了
Ⅲ 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文件,登錄界面
<%@pagepageEncoding="utf-8"contentType="text/html;charset=utf-8"%>
<body>
<h1>Register</h1>
<hr/>
<formaction="login"method="post">
用戶名:
<inputtype="text"name="username"id="text"/>
密碼:
<inputtype="password"name="pwd"id="text"//>
<inputtype="submit"value="登錄"id="button"/>
</form>
</body>
//這是server文件,web.xml部署名字login
importjava.io.IOException;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
{
publicvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
Stringusername=request.getParameter("username");
Stringpwd=request.getParameter("pwd");
try{
if(username.equals("name")&&pwd.equals("pwd")){
system.out.print("登錄成功");
}else{
system.out.print("error");
}
}catch(Exceptione){
e.printStackTrace();
}
}
}
簡單的登錄界面,不需要資料庫,如果要鏈接資料庫就要判斷很多了
Ⅳ 編寫 JSP 頁面,界面中顯示 1~9,9 個鏈接,單擊每個鏈接,能夠在另一個頁面列印該數 字的平方。
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>test.jsp</title>
</head>
<body>
<%
String s = null;
if ((s = request.getParameter("num")) != null) {
int i = Integer.parseInt(s);
out.println((i * i) + "<br />");
}
%>
<%
for (int i = 1; i < 10; i++) {
%>
<a href="test.jsp?num=<%=i%>"><%=i%></a>
<%
}
%>
</body>
</html>
Ⅵ jsp登陸界面源代碼
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>
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();
}
}
}
Ⅶ 如何在eclipse中運行jsp頁面
使用eclipse編寫並運行jsp程序的步驟
1、點擊File->New->Project,在出現的菜單中選擇Web->Dynamic Web Project,點擊next
2、在Project name中填寫工程名稱,在target runtime中點擊New runtime,選擇自己安裝的版本的tomcat,點擊Finish
3、在左邊的Project Explorer中可以找到新建立的工程MyJsp,在工程中找到WebContent
4、右鍵WebContent,new->jsp file,在出現的窗口,在file name欄可以更改文件名,finish
5、然後就可以編寫jsp文件了,需要注意的是,在生成的部分代碼中,要將charset後的參數改成「utf-8」,否則顯示中文會出現亂碼
6、編寫jsp文件,實現想要實現的功能
7、點擊工具欄中間的綠色按鈕即可運行
Ⅷ 鐢╩yeclipse杞浠剁紪鍐欑殑涓涓猨sp鐣岄潰 鐢╨ocalhost鍔犺澆涓嶅嚭鏉 鎶500鐨勯敊璇錛
500閿欒涓鑸鏄鏈嶅姟鍣ㄥ唴閮ㄩ敊璇錛屽彲鑳芥槸鎮ㄧ殑JSP鏂囦歡鏈夎娉曢敊璇鎴栬呭叾浠栭棶棰樺艱嚧鐨勬湇鍔″櫒鏃犳硶姝g『榪愯孞SP欏甸潰銆備互涓嬪彲浠ュ皾璇曡В鍐寵ラ棶棰樼殑姝ラわ細
媯鏌JSP鏂囦歡涓鏄鍚︽湁璇娉曢敊璇銆傚湪myeclipse涓榪涜屽紑鍙戞椂錛屽嵆浣挎病鏈夌紪璇戦敊璇錛孞SP鏂囦歡涔熸湁鍙鑳藉瓨鍦ㄨ娉曢敊璇銆傚緩璁浣跨敤Eclipse鑷甯︾殑JSP緙栬緫鍣ㄦ垨鍏朵粬JSP緙栬緫鍣ㄦ鏌ヤ唬鐮佺殑姝g『鎬э紝騫舵煡鐪嬫帶鍒跺彴杈撳嚭鐨勮︾粏閿欒淇℃伅銆
媯鏌Servlet瀹瑰櫒鎴朩eb鏈嶅姟鍣ㄦ槸鍚︽g『閰嶇疆銆傚傛灉Servlet瀹瑰櫒鎴朩eb鏈嶅姟鍣ㄦ病鏈夋g『閰嶇疆錛屽垯鍙鑳藉艱嚧JSP欏甸潰鏃犳硶鍔犺澆銆傚彲浠ユ鏌MyEclipse涓鏄鍚︽湁姝g『閰嶇疆Tomcat絳塖ervlet瀹瑰櫒銆傚悓鏃訛紝榪橀渶瑕佺『淇漈omcat鎴栧叾浠朩eb鏈嶅姟鍣ㄥ凡緇忓惎鍔ㄦe父銆
媯鏌URL璺寰勬槸鍚︽g『銆傚傛灉鎮ㄧ殑JSP欏甸潰浣嶄簬欏圭洰鐨勫瓙鐩褰曚腑錛岄偅涔堥渶瑕佺『淇漊RL璺寰勬g『錛屼緥濡傦細http://localhost:8080/yourproject/subdirectory/index.jsp
媯鏌jsp欏甸潰鏄鍚︽g『鏀劇疆鍦╓EB-INF鐩褰曚笅銆傚傛灉jsp鏂囦歡娌℃湁鏀劇疆鍦╓EB-INF鐩褰曚笅錛岃屾槸鐩存帴鏀懼湪WebContent鐩褰曚笅闈錛岄偅涔堜篃浼氬艱嚧500閿欒銆
媯鏌ラ」鐩涓鏄鍚︽湁緙哄け鐨刯ar鍖呮垨鑰呯被銆傚傛灉浣跨敤浜嗕竴浜涚壒鍒鐨勭被搴撴垨妗嗘灦錛岄渶瑕佺『淇濈浉鍏崇殑jar鍖呭凡緇忔g『娣誨姞鍒伴」鐩鐨刢lasspath涓銆
濡傛灉浠ヤ笂鏂規硶閮芥病鏈夎В鍐抽棶棰橈紝榪樺彲浠ュ皾璇曢噸鍚痬yeclipse鍜宼omcat錛屾垨鑰呴噸鏂伴儴緗查」鐩銆傚笇鏈涜繖浜涘緩璁鑳藉熷府鍔╀綘瑙e喅闂棰樸