Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. <form action="addAnimal" method="post">
  2. <fieldset>
  3. <legend>What is Your Favorite Pet?</legend>
  4. <input type="checkbox" name="animal" value="Cat"/>Cats <br />
  5. <input type="checkbox" name="animal" value="Dog" />Dogs<br />
  6. <input type="checkbox" name="animal" value="Bird" />Birds<br />
  7. <input type="submit" value="Submit now" />
  8. </fieldset>
  9. </form>
  10.  
  11. package com.bean;
  12.  
  13. import com.dao.AnimalDao;
  14. import com.opensymphony.xwork2.ActionSupport;
  15.  
  16. public class Animal extends ActionSupport {
  17.  
  18. private String animal;
  19.  
  20. public String getAnimal() {
  21. return animal;
  22. }
  23.  
  24. public void setAnimal(String animal) {
  25. this.animal = animal;
  26. }
  27.  
  28. public String execute(){
  29. AnimalDao.addAnimal(this);
  30. return SUCCESS;
  31. }
  32. }
  33.  
  34. import java.util.Map;
  35.  
  36. import org.apache.struts2.dispatcher.SessionMap;
  37. import org.apache.struts2.interceptor.SessionAware;
  38.  
  39. import com.bean.Animal;
  40.  
  41. import com.connection.DatabaseConnection;
  42. import com.opensymphony.xwork2.ActionSupport;
  43.  
  44. public class AnimalDao extends ActionSupport implements SessionAware {
  45.  
  46. SessionMap<String,Object> map;
  47. public static void addAnimal(Animal animal) {
  48. // TODO Auto-generated method stub
  49. try{
  50. Connection con=DatabaseConnection.Dbconnection();
  51. String sql="insert into chk values(?)";
  52. PreparedStatement ps=con.prepareStatement(sql);
  53. ps.setString(1,animal.getAnimal());
  54. ps.executeUpdate();
  55.  
  56. }catch(Exception e){
  57.  
  58. }
  59.  
  60. }
  61.  
  62. public String retriveAnimal(){
  63. ArrayList<Animal> aList=new ArrayList<Animal>();
  64. try{
  65. Connection con=DatabaseConnection.Dbconnection();
  66. String sql="select * from chk";
  67. PreparedStatement ps=con.prepareStatement(sql);
  68. ResultSet rs=ps.executeQuery();
  69. while(rs.next()){
  70. Animal a=new Animal();
  71. a.setAnimal(rs.getString(1));
  72. aList.add(a);
  73. map.put("aList",aList);
  74. }
  75.  
  76. }catch(Exception e){
  77. System.out.println(e);
  78. }
  79. return SUCCESS;
  80. }
  81.  
  82. @Override
  83. public void setSession(Map<String, Object> map) {
  84. // TODO Auto-generated method stub
  85. this.map=(SessionMap<String, Object>) map;
  86.  
  87. }
  88.  
  89. }
  90.  
  91. <action name="addAnimal" class="com.bean.Animal">
  92.  
  93. <result name="success">index.jsp</result>
  94.  
  95. </action>
  96.  
  97. <action name="chkbox" class="com.dao.AnimalDao" method="retriveAnimal">
  98.  
  99. <result name="success">viewuser.jsp</result>
  100.  
  101. </action>
  102.  
  103. <s:iterator value="#session.aList" var="stat">
  104. <s:checkbox name="animal[%{#stat.index}]" value=""/>
  105. </s:iterator>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement