本文目錄一覽:
- 1、jsp+ssh編程
- 2、需要一個可以運行的JSP簡單代碼?
- 3、初學jsp中的ssh框架
- 4、求ssh2 項目代碼(特別是一定要有購物車,或者單純的購物車和jsp也可以參考)
- 5、SSH問題:管理添加頁面/web-root/jsp/admin/admin_insert.jsp代碼如下:
- 6、求一個jsp代碼實現登錄,驗證消息就為用戶名和密碼
jsp+ssh編程
@1f89f1d4這是內存地址
list.size() 0 就代表用戶名和密碼同時正確(username不能重複的情況)
需要一個可以運行的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中的ssh框架
這麼跟你說吧,SSH 是3個框架,一個是管和資料庫打交道的,就是存數據,取數據的,是其中的H,即hibernate。
一個是管業務的,即struts2,也就是比如你在網頁點一個按鈕,後面應該給你顯示那些數據,而這個時候,hibernate只管查,而怎麼去查,是struts2管的。
一個是開源的框架,即spring,這個傢伙主要是作用是依賴注入,說白點,就是你的程序里不需要出現任何常量,比如new Student() ,比如”hello” 等等 ,這些都可以靠配置文件來搞定了。
比如說你寫了個字元串 ,是hello,後來你要改,要byebye了,如果沒有依賴注入的話,你要去改程序的,打開程序,把hello改成byebye,這樣的話就不好了,有了spring,你改個配置文件,搞定!具體怎麼改的,在哪寫的,這些我就不是一兩句能說清楚了,而且我這也只是舉個例子。
另外spring還給很多框架提供了支持,什麼叫提供支持,就是spring裡面有這些框架的支持類,你可以直接用,比如hibernate,就有這麼一個類,hibernateDaoSupport,比如struts2,就有這麼一個類ActionSupport。當然 還有其他很多現成的,寫好的類給你用。
這就是叫提供支持!
我覺得我說的夠白話了吧。
最後說說框架和設計模式,設計模式是一種設計的理念,說白點,設計模式就是模式,沒有代碼。
而框架,是已經寫好的,具有一定功能的代碼,框架一般都封裝了很多現成的類,你可以直接使用,非常方便。
希望對你有幫助。
求ssh2 項目代碼(特別是一定要有購物車,或者單純的購物車和jsp也可以參考)
我以前做過,很全的一個!你找下你需要的代碼吧,
//發票集合 id
public static int receiptids;
//購物車集合Linked
public static MapString ,Goodslist ShoppingMap =null;
//獲取集合
public MapString, Goodslist getShoppingMap() {
if(ShoppingMap==null){
//System.out.println(“get==null”);
return ShoppingMap=new HashMapString,Goodslist();
}else{
return ShoppingMap;
}
}
public void setShoppingMap(MapString, Goodslist shoppingMap) {
ShoppingMap = shoppingMap;
}
//添加商品
public void addBookMap(Goodslist shoop){
if(ShoppingMap==null){
ShoppingMap=new HashMapString,Goodslist();
ShoppingMap.put(shoop.getGname(),shoop);
}else{
this.ShoppingMap.put(shoop.getGname(),shoop);
}
System.out.println(ShoppingMap.size());
}
//刪除集合中的key
public void remBookMap(String key){
if(this.ShoppingMap.containsKey(key)){
this.ShoppingMap.remove(key);
}
}
//獲取商品集合
public Goodslist getBookMap(String key){
return (Goodslist)this.ShoppingMap.get(key);
}
//購物車管理
public MapString ,Goodslist ManagerAddShopping(Goodslist goodslist){
//添加購商品到購物車
String goodsName=goodslist.getGname();
if(goodslist!=null){
if(this.getShoppingMap().containsKey(goodsName)){
//得得單個商品數量 //如果有則加一
int count=ShoppingMap.get(goodsName).getGnumber();
ShoppingMap.get(goodsName).setGnumber(count+1);
//得到單個商品總價
ShoppingMap.get(goodsName).setSubtotal(goodslist.getPrice()*(count+1));
}else{
System.out.println(“新添加商品到購物車”);
//添加一個商品集合到購物車
this.addBookMap(goodslist);
}
return ShoppingMap;
}
return null;
}
//修改數量
public void updateShopping(String key,int number){
System.out.println(“key”+key+number);
if(this.getShoppingMap().containsKey(key)){
//加
if(number0){
//得得單個商品數量 //如果有則加一
//int count=ShoppingMap.get(key).getGnumber();
ShoppingMap.get(key).setGnumber(number);
//得到單個商品總價
ShoppingMap.get(key).setSubtotal(ShoppingMap.get(key).getPrice()*(number));
}
}
}
//計價
public double getSumPirce(){
double sumMoney=0;
if(this.ShoppingMap.size()0){
for(Map.EntryString, Goodslist entry : ShoppingMap.entrySet()) {
System.out.println(entry.getKey());
sumMoney=sumMoney+ShoppingMap.get(entry.getKey()).getSubtotal();
}
/*Iterator it =ShoppingMap.keySet().iterator();
while(it.hasNext()){
String key=(String)it.next();
}
*/
}
return sumMoney;
}
//模糊搜索
public List getSerch(String name){
List list=null;
if(name.isEmpty()){
System.out.println(“Manager.getSerch() return null”);
return null;
}else{
//list=imples.getSeachResults(name);
return list;
}
}
/*
*讀取 訂單人的收貨地址 根據會員id
*/
public ListMemberdatas getConsigneeInfo(int id){
String sql=”from Memberdatas where MId=”+id;
return ExcuteSQL.executQuery(sql, null);
}
/*
*讀取 訂單人的收貨地址 根據編號
*/
public ListMemberdatas getConsigneeInfoID(int id){
String sql=”from Memberdatas where miId=”+id;
return ExcuteSQL.executQuery(sql, null);
}
/*
*讀取 訂單人的商品集合
*/
public ListGoods getConsigneeGoods(){
return null;
}
/*
*讀取 訂單人的發票信息
*/
public ListReceipt getReceipt(int meberID){
String sql=”from Receipt where OId=”+meberID;
return ExcuteSQL.executQuery(sql, null);
}
/*
*插入 訂單人的收貨地址
*/
public boolean inisertConsigneeInfo(Memberdatas memberdatas){
boolean b=ExcuteSQL.save(memberdatas);
return b;
}
//刪除收貨人地址
public boolean delectMebreAddes(int mid){
String sql=”delete Memberdatas where miId=”+mid;
//System.out.println(“ManagerOrder.delectMebreAddes()=”+mid);
int count=ExcuteSQL.executUpdate(sql, null);
if(count0){return true;}else{ return false;}
}
//修改收貨人地址
public boolean updateAdders(int mlid,String [] whe){
//String sql=”update Memberdatas set names=?,adders=?, phone=? , phones=? where miId=”+mlid;
String sql=”update Memberdatas set names='”+whe[0]+”‘,adders='”+whe[1]+”‘, phone='”+whe[2]+”‘, phones='”+whe[3]+”‘ where miId=”+mlid;
int count=ExcuteSQL.executUpdate(sql, null);
if(count0){return true;}else{ return false;}
}
//修改發票
public boolean updateReli(int ids,String rname, String rtype){
String sql=”update Receipt set rtype='”+rtype+”‘ ,retop='”+rname+”‘ where ids=”+ids;
int count=ExcuteSQL.executUpdate(sql, null);
if(count0){return true;}else{ return false;}
}
//添加發票
public boolean insertReli(Receipt re){
return ExcuteSQL.save(re);
}
//插入商品列表
public boolean insertGoodsList(Goodslist golist)
{
return ExcuteSQL.save(golist);
}
//臨時商品集合 從購物車中取出的商品
public static ListGoodslist TempList;//=new ArrayListGoodslist();
public void addTempList(Goodslist go){
if(TempList==null){
TempList=new ArrayListGoodslist();
}
TempList.add(go);
}
public ListGoodslist getTempList() {
if(TempList==null){
return TempList=new ArrayListGoodslist();
}else{
return TempList;
}
}
public void setTempList(ListGoodslist tempList) {
TempList = tempList;
}
public ListReceipt reList;
public ListReceipt getReList() {
if(reList==null){
return reList=new ArrayListReceipt();
}else{
return reList;
}
}
public void setReList(ListReceipt reList) {
if(reList==null){
reList=new ArrayListReceipt();
}
this.reList = reList;
}
public void addreList(Receipt re){
if(reList==null){
reList=new ArrayListReceipt();
}
reList.add(re);
}
//查詢訂單根據會員id
public ListGoodsOrader getOrder(int mid){
//先獲取訂單id
ListGoodsOrader listRetu=new ArrayListGoodsOrader();
Interfaces imp=new Imple();
List ids=imp.getOrderid(mid);
System.out.println(“ManagerOrder.getOrder()”+ids);
if(ids!=null){
for(int i=0;iids.size();i++){
System.out.println(“ManagerOrder.getOrder()ids.size()=”+ids.size());
String sql=”from GoodsOrader where orders.OId=”+Integer.parseInt(ids.get(i).toString());
ListGoodsOrader list=ExcuteSQL.executQuery(sql, null);
for(GoodsOrader god:list){
//添加訂單對象,
listRetu.add(god);
}
}
return listRetu;
}else{
return null;
}
}
//刪除訂單
public boolean deleteOrder(int oid){
String sql=”delete GoodsOrader where orders.OId=”+oid;
int b=ExcuteSQL.executUpdate(sql, null);
if(b0) return true;else return false;
}
}
————————————————————————-
package snail.su.rui.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import snail.su.manager.ManagerOrder;
import snail.su.vo.Goodslist;
public class UpdateShoop extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html;charset=utf-8”);
PrintWriter out = response.getWriter();
//修改購物車
String shoopKey=request.getParameter(“shoopKey”);
String number=request.getParameter(“number”);
String shookeys=new String(shoopKey.getBytes(“ISO-8859-1″),”gb2312”);
ManagerOrder order=new ManagerOrder();
order.updateShopping(shookeys, Integer.parseInt(number));
request.getSession().setAttribute(“buyNumber”,order.ShoppingMap.size());
//計價
double sunMoney=order.getSumPirce();
out.print(sunMoney);
System.out.println(“UpdateShoop.doPost()”+sunMoney);
}
}
—————————————————–
//刪除購物車裡的物品
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html;charset=utf-8”);
PrintWriter out = response.getWriter();
String key=request.getParameter(“key”);
key=new String(key.getBytes(“ISO-8859-1″),”utf-8”);
ManagerOrder orader=new ManagerOrder();
orader.remBookMap(key);
request.getSession().setAttribute(“buyNumber”,orader.ShoppingMap.size());
//獲取商品集合到購物車
ManagerOrder order =new ManagerOrder();
MapString ,Goodslist shoopMap=order.getShoppingMap();
//添加集合到session
HttpSession ShoopSession=request.getSession();
ShoopSession.setAttribute(“SUshoopMap”,shoopMap);
System.out.println(shoopMap.size());
//把session寫入到cookie裡面
if(ShoopSession!=null){
System.out.println(“寫入session到了cookie了!”);
Cookie cookie=new Cookie(“JSESSIONID”,ShoopSession.getId());
cookie.setMaxAge(60*60*24);
response.addCookie(cookie);
}
request.getRequestDispatcher(“WEB-INF/rui/myShoop.jsp”).forward(request, response);
}
SSH問題:管理添加頁面/web-root/jsp/admin/admin_insert.jsp代碼如下:
在你的struts.xml中相應的action標籤中加一屬性method=”insert”
action
method=”insert”
attribute=”adminForm”
input=”/jsp/errors.jsp”
name=”adminForm”
parameter=”status”
path=”/jsp/admin/admin”
scope=”request”
type=”org.lxh.myzngt.struts.action.AdminAction”
forward name=”insertdo” path=”/jsp/admin/admin_insertdo.jsp”/forward
forward name=”list” path=”/jsp/admin/admin_list.jsp”/forward
forward name=”updatepwddo” path=”/jsp/admin/admin_updatepwd_do.jsp”/forward
forward name=”deletedo” path=”/jsp/admin/admin_delete_do.jsp”/forward
/action
一般action都會繼承ActionSupport,所以會默認執行excute方法,我們的邏輯處理一般會寫在action中這個重寫的方法內,你如果要用其他方法就需要在xml中說明
求一個jsp代碼實現登錄,驗證消息就為用戶名和密碼
你是想用那種方法實現呢? 是單單 jsp+javabean 還是 jsp+struts2 還是 ssh 框架?先給你個 jsp+struts2 的例子吧,我之前做過的,你只用看對你有用的部分
//登錄頁面,form 表單
s:form action=”deng” method=”post”
table
tr
tdimg src=”image/bg4.jpg” height=”550″ width=”450″ border=”0″/td
td
fieldset style=”width: 260px; height: 190px;”
legendh2會員登錄/h2/legend
姓 名:input type=”text” name=”name”/br/br/
密 碼:input type=”password” name=”password”/br/br/br/
input type=”submit” value=”登 錄”
s:a href=”regist.jsp”立即註冊/s:a/fieldset
/td
/tr
/table
/s:form
//配置 struts.xml 文件
package name=”mu” extends=”struts-default”
!– 會員登錄 —
action name=”deng” class=”action.LoginAction” method=”loginUser”
result/user_first.jsp/result
result name=”failure”/login.jsp/result
/action
/package
//下面是處理的 action :
package action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import dbOperate.AdminDAO;
import dbOperate.UserDAO;
@SuppressWarnings(“serial”)
public class LoginAction extends ActionSupport {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//會員登錄
public String loginUser()
{
if((new UserDAO()).login_Action(name,password))
{
ActionContext ac = ActionContext.getContext();
ac.getSession().put(“username”, name);
return SUCCESS;
}
else
{
this.addActionError(“用戶名或密碼錯誤,登錄失敗!”);
return “failure”;
}
}
//管理員登錄
public String loginAdmin()
{
if((new AdminDAO()).login_Action(name, password))
{
ActionContext ac = ActionContext.getContext();
ac.getSession().put(“username”, name);
return SUCCESS;
}
else
{
this.addActionError(“用戶名或密碼錯誤,登錄失敗!”);
return “failure”;
}
}
}
//下面是處理的 java 類
package dbOperate;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import bean.RegUser;
public class UserDAO {
DbConnection dbConnect = null;
Connection con = null;
java.sql.PreparedStatement ps = null;
private RegUser user;
public RegUser getUser() {
return user;
}
public void setUser(RegUser user) {
this.user = user;
}
//會員登錄
public boolean login_Action(String name,String password)
{
boolean b = false;
ResultSet rs = null;
try
{
dbConnect = new DbConnection();
con = dbConnect.getConnection();
String sql = “select password from reguser where name = ?”;
ps = con.prepareStatement(sql);
ps.setString(1,name);
rs = ps.executeQuery();
while(rs.next())
{
if(rs.getString(“password”).equals(password))
b = true;
}
}
catch (Exception e)
{
System.out.println(“出錯信息:”+e.getMessage());
}
finally
{
try {
if (con!=null)
con.close();
} catch (Exception e2) {}
}
return b;
}
}
希望對你有幫助
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/232522.html