2

I have an HTML page that takes text input of the file path and that input process by the getOwner() of a Detail class in servlet. This servlet creates an HTML dropdown menu with all the local users in a computer fetched from the getACL() in the same servlet. Now if a user selects a specific user and the checkbox having different permission, how to get the selected value in the dropdown menu and checkbox to be processed by modifyACL() in Detail class.

public class Details {
    native String[] getACL(String s);
    native String getOwner(String name); 
    native void modifyACL(String s, String k, int i);
    static{
      System.loadLibrary("Details");
   }}

Servlet class


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ACL extends HttpServlet{
    
    private static final long serialVersionUID = 1L;
    
    public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException  
    {  
       Details obj =new Details();
       int len;
       System.load("C:\\Users\\pradeep-pt3689\\eclipse-workspace\\FilePermission\\jni\\Details.dll");
       String pathDir = req.getParameter("path");
       res.setContentType("text/html");//setting the content type
       PrintWriter pw=res.getWriter();//get the stream to write the data  
       pw.println("<html><body><center>");
       pw.println("Welcome to servlet,File owner is: <b>");
       pw.println(obj.getOwner(pathDir));
       pw.println("</b><br><br>");
       String[] prop = obj.getACL(pathDir);
       pw.println("The File property is ::<br><table><tr><th>User or Group Name</th><th>Permission</th></tr>");
       len=prop.length;
       String[] arr = new String[len];
       for(int i=0;i<len;i++)
       {
         String[] parts=prop[i].split("\\\\");
         arr[i]=parts[0]+"\\"+parts[1];
         pw.println("<tr><td>");
         pw.println(arr[i]);
         pw.println("</td><td>");
         if(parts[2].contains("Full Control"))
         {
             pw.println("<label><input type='checkbox' name='property' value='FullControl' checked='checked'/>Full Control</label>"
                    + "<label><input type='checkbox' name='property' value='Read'/>Read</label>"
                    + "<label><input type='checkbox' name='property' value='Write'/>Write</label>"
                    + "<label><input type='checkbox' name='property' value='Execute'/>Execute</label>");
         } 
         else
         {
            pw.println("<label><input type='checkbox' name='property' value='FullControl'/>Full Control</label>");
            if(parts[2].contains("Read")){
                pw.println("<label><input type='checkbox' name='property' value='Read' checked='checked'/>Read</label>");
            }
            if(parts[2].contains("Execute")) {
                pw.println("<label><input type='checkbox' name='property' value='Read' checked='checked'/>Execute</label>");
            }
            if(parts[2].contains("Write")) {
                pw.println("<label><input type='checkbox' name='property' value='Read' checked='checked'/>Write</label>");
            }
         }
         pw.println("<td></tr>");
       }
       pw.println("</table>");
       pw.println("<hr>");
       pw.println("Enter details to modify ACL<br><br>");
       pw.println("<label for='user'>Select a user:</label><select name='user'>");
       for(int j=0;j<len;j++) {
           pw.println("<option value='");
           pw.println(arr[j]);
           pw.println("'>");
           pw.println(arr[j]);
           pw.println("</option>");
       }
       pw.println("</select>&nbsp;");
       pw.println("<label><input type='checkbox' name='modifyProperty' value='FullControl' checked='checked'/>Full Control</label>"
            + "<label><input type='checkbox' name='modifyProperty' value='Read'/>Read</label>"
            + "<label><input type='checkbox' name='modifyProperty' value='Write'/>Write</label>"
            + "<label><input type='checkbox' name='modifyProperty' value='Execute'/>Execute</label>");
       pw.println("</center></body></html>");
       pw.close();//closing the stream  
    }
}

output

I have to get selected value of second part in given page image.

Margie
  • 57
  • 11
  • You need to pass the selected value of user and checkbox in `modifyACL..` ? If yes you need to submit that then you can access the same – Swati Aug 04 '20 at 14:01
  • yeah exactly. How to get those values? Since it's not defined as a separate JSP page element sending those values for processing. @Swati – Margie Aug 04 '20 at 14:17
  • 1
    put your inputs under `form` tags i.e : `out.println("
    ");...//your select-box and checkbox out.println("
    ");` . Also add a submit button inside `form` and get value in `doPost` method of servlet and write your modification code there.But , i would suggest you to write these HTML codes in jsp not servlet .
    – Swati Aug 04 '20 at 14:23
  • Thanks a lot. This seems to be work. I will try and let know. BTW is there any other way like sending the string array as a parameter in a different jsp page and then creating a drop-down menu there using this array parameter value? – Margie Aug 04 '20 at 14:26
  • 1
    Yes you can do that as well . Put that array value in `request.setAttribute()` and pass the same to jsp page .Also to access the same in jsp page use [jstl](https://stackoverflow.com/questions/2148658/iterate-over-elements-of-list-and-map-using-jstl-cforeach-tag) – Swati Aug 04 '20 at 14:29
  • For populating the drop down value in jsp shoul I use something like this request.setAttribute("courses", courses); Is this will be correct? @Swati – Margie Aug 04 '20 at 14:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219201/discussion-between-swati-and-margie). – Swati Aug 04 '20 at 14:43

0 Answers0