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喅闂棰樸